package com.alibaba.citrus.cr.mn.order.center.forecastorder.ability;

import com.alibaba.citrus.cr.common.enums.ordercenter.*;
import com.alibaba.citrus.cr.common.exception.FunctionException;
import com.alibaba.citrus.cr.common.util.DateUtils;
import com.alibaba.citrus.cr.mn.order.center.common.util.AssertUtils;
import com.alibaba.citrus.cr.mn.order.center.common.util.JsonUtils;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.BaseDataServiceAdapter;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.BusinessUnitAbilityAdapter;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.DictionaryVerifyAdapter;
import com.alibaba.citrus.cr.mn.order.center.facade.repository.ForecastOrderQueryRepository;
import com.alibaba.citrus.cr.mn.order.center.forecastorder.convert.ForecastOrderLineSDOToCreateForecastDetailResponseDTOConvertor;
import com.alibaba.citrus.cr.mn.order.center.forecastorder.entity.ForecastOrderEntity;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.epoch.app.crforecastsales.bo.ForecastOrderBO;
import com.epoch.app.crforecastsales.bo.ForecastOrderLineBO;
import com.epoch.app.crforecastsales.domian.forecastorder.dto.QueryBySourceNoRequest;
import com.epoch.app.crforecastsales.sdo.ForecastOrderSDO;
import com.epoch.app.mnordercenter.contants.MnOrderCenterConstants;
import com.epoch.app.mnordercenter.model.dto.*;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service("forecastOrderAbility")
public class ForecastOrderAbility {
    private static final Log log = Log.getLogger(ForecastOrderAbility.class);

    public static final String INTEGER_MAX_VALUE = "2147483647";

    @Resource
    private ForecastOrderQueryRepository forecastOrderQueryRepository;
    @Resource
    private DictionaryVerifyAdapter dictionaryVerifyAdapter;
    private static ForecastOrderAbility forecastOrderAbility;
    @Resource
    private BaseDataServiceAdapter baseDataServiceAdapter;
    @Resource
    private BusinessUnitAbilityAdapter businessUnitAbilityAdapter;

    @PostConstruct
    public void init() {
        forecastOrderAbility = this;
        forecastOrderAbility.dictionaryVerifyAdapter = dictionaryVerifyAdapter;
        forecastOrderAbility.baseDataServiceAdapter = baseDataServiceAdapter;
        forecastOrderAbility.businessUnitAbilityAdapter = businessUnitAbilityAdapter;
    }

    public static void baseParamsCheck(String fromSys, String businessType, String businessUnitCode) {
        // 必填校验
        AssertUtils.isTrue(StringUtils.isBlank(fromSys), "OC-01-001-01-15-001", "来源系统编码");
        AssertUtils.isTrue(StringUtils.isBlank(businessType), "OC-01-001-01-15-001", "业态编码");
        AssertUtils.isTrue(StringUtils.isBlank(businessUnitCode), "OC-01-001-01-15-001", "业务单元编码");

        // 长度校验
        AssertUtils.lengthCheck(fromSys, 50, true, "OC-01-001-01-15-004", "来源系统编码");
        AssertUtils.lengthCheck(businessType, 10, true, "OC-01-001-01-15-004", "业态编码");
        AssertUtils.lengthCheck(businessUnitCode, 50, true, "OC-01-001-01-15-004", "业务单元编码");

        // 枚举值校验
        forecastOrderAbility.dictionaryVerifyAdapter.verifyDictionary(DictionaryTypeEnum.FROM_SYS.getCode(), fromSys, new FunctionException("OC-01-001-01-15-006", "来源系统编码"));
        forecastOrderAbility.dictionaryVerifyAdapter.verifyDictionary(DictionaryTypeEnum.BUSINESS_TYPE.getCode(), businessType, new FunctionException("OC-01-001-01-15-006", "业态编码"));
        AssertUtils.isTrue(!forecastOrderAbility.businessUnitAbilityAdapter.checkBusinessUnitCode(businessUnitCode), "OC-01-001-01-15-006", "业务单元编码");
    }

    public static void createParamsInvalid(List<ForecastOrderEntity> forecastOrderEntities) {
        // 空值校验
        paramsNotBlankCheck(forecastOrderEntities);
        // 长度校验
        paramsLengthCheck(forecastOrderEntities);
        // 枚举校验（合法性校验）
        paramsEnumDictCheck(forecastOrderEntities);
    }

    public static void analysisParamsInvalid(AnalysisForecastRequest analysisForecastRequest) {
        //should throw exception
        // 空值校验
        analysisParamsNotBlankCheck(analysisForecastRequest);

        // 长度校验
        analysisParamsLengthCheck(analysisForecastRequest);

        // 枚举校验（合法性校验）
        analysisParamsEnumDictCheck(analysisForecastRequest);
    }

    private static void analysisParamsEnumDictCheck(AnalysisForecastRequest analysisForecastRequest) {

        forecastOrderAbility.dictionaryVerifyAdapter.verifyDictionary(DictionaryTypeEnum.FROM_SYS.getCode(), analysisForecastRequest.getFromSys(), new FunctionException("OC-01-001-01-15-006", "来源系统编码"));
        forecastOrderAbility.dictionaryVerifyAdapter.verifyDictionary(DictionaryTypeEnum.BUSINESS_TYPE.getCode(), analysisForecastRequest.getBusinessType(), new FunctionException("OC-01-001-01-15-006", "业态编码"));
    }

    private static void analysisParamsLengthCheck(AnalysisForecastRequest analysisForecastRequest) {
        AssertUtils.isNumber(analysisForecastRequest.getBusinessType(), true, "OC-01-001-01-15-006", "业态编码");
        // 必填校验
        AssertUtils.lengthCheck(analysisForecastRequest.getFromSys(), 50, true, "OC-01-001-01-15-004", "来源系统编码");
        AssertUtils.lengthCheck(analysisForecastRequest.getBusinessType(), 10, true, "OC-01-001-01-15-004", "业态编码");
        AssertUtils.lengthCheck(analysisForecastRequest.getModifierCode(), 50, true, "OC-01-001-01-15-004", "修改人工号");
        AssertUtils.lengthCheck(analysisForecastRequest.getBusinessUnitCode(), 50, true, "OC-01-001-01-15-004", "业务单元编码");
        analysisForecastRequest.getAnalysisForecastNoDTOList().forEach(analysisForecastBizDTO -> {
            AssertUtils.lengthCheck(analysisForecastBizDTO.getLineNum(), 10, true, "OC-01-001-01-15-004", "行号");
            AssertUtils.lengthCheck(analysisForecastBizDTO.getSalesOrderNo(), 50, true, "OC-01-001-01-15-004", "预报订单单号");
        });

    }

    private static void analysisParamsNotBlankCheck(AnalysisForecastRequest analysisForecastRequest) {
        AssertUtils.isTrue(Objects.isNull(analysisForecastRequest), "OC-01-001-01-15-001", "单据列表");
        AssertUtils.isTrue(StringUtils.isBlank(analysisForecastRequest.getFromSys()), "OC-01-001-01-15-001", "来源系统编码");
        AssertUtils.isTrue(StringUtils.isBlank(analysisForecastRequest.getBusinessType()), "OC-01-001-01-15-001", "业态编码");
        AssertUtils.isTrue(StringUtils.isBlank(analysisForecastRequest.getModifierCode()), "OC-01-001-01-15-001", "修改人工号");
        AssertUtils.isTrue(StringUtils.isBlank(analysisForecastRequest.getBusinessUnitCode()), "OC-01-001-01-15-001", "业务单元编码");
        AssertUtils.isTrue(CollectionUtils.isEmpty(analysisForecastRequest.getAnalysisForecastNoDTOList()), "OC-01-001-01-15-001", "analysisForecastNoDTOList");
        AssertUtils.isTrue(analysisForecastRequest.getAnalysisForecastNoDTOList().size() > 100, "OC-01-001-01-15-004", "createForecastOrderDTOS", analysisForecastRequest.getAnalysisForecastNoDTOList().size(), "100");
        analysisForecastRequest.getAnalysisForecastNoDTOList().forEach(analysisForecastBizDTO -> {
                    AssertUtils.isTrue(StringUtils.isBlank(analysisForecastBizDTO.getLineNum()), "OC-01-001-01-15-001", "行号");
                    AssertUtils.isTrue(StringUtils.isBlank(analysisForecastBizDTO.getSalesOrderNo()), "OC-01-001-01-15-001", "预报订单单号");
                }
        );
    }

    public static void confirmParamsInvalid(ConfirmReplyRequest confirmReplyRequest) {
        // 空值校验
        confirmParamsNotBlankCheck(confirmReplyRequest);

        // 长度校验
        confirmParamsLengthCheck(confirmReplyRequest);

        // 枚举校验（合法性校验）
        confirmParamsEnumDictCheck(confirmReplyRequest);
    }

    public static void confirmGroupParamsInvalid(ConfirmGroupReplyRequest confirmGroupReplyRequest) {
        ConfirmReplyRequest confirmReplyRequest = new ConfirmReplyRequest();
        BeanUtils.copyProperties(confirmGroupReplyRequest, confirmReplyRequest);
        // 空值校验
        confirmParamsNotBlankCheck(confirmReplyRequest);

        // 长度校验
        confirmParamsLengthCheck(confirmReplyRequest);

        // 枚举校验（合法性校验）
        confirmParamsEnumDictCheck(confirmReplyRequest);

        //预报订单号和行号唯一性校验
        confirmParamsUniquenessCheck(confirmReplyRequest);
    }

    public static void confirmSyncGroupParamsInvalid(ConfirmReplyFulfillRequest confirmGroupReplyRequest) {
        ConfirmReplyRequest confirmReplyRequest = new ConfirmReplyRequest();
        BeanUtils.copyProperties(confirmGroupReplyRequest, confirmReplyRequest);
        // 空值校验
        confirmParamsNotBlankCheck(confirmReplyRequest);

        // 长度校验
        confirmParamsLengthCheck(confirmReplyRequest);

        // 枚举校验（合法性校验）
        confirmParamsEnumDictCheck(confirmReplyRequest);

        //预报订单号和行号唯一性校验
        confirmParamsUniquenessCheck(confirmReplyRequest);
    }

    private static void confirmParamsUniquenessCheck(ConfirmReplyRequest confirmReplyRequest) {
        Set<String> salesOrderNoLineNumSet = new HashSet<>();
        confirmReplyRequest.getConfirmReplyList().forEach(analysisForecastBizDTO -> {
            //行号校验
            AssertUtils.isWholeNumber(analysisForecastBizDTO.getLineNum(),Boolean.TRUE,"OC-00-001-01-15-001","行号");
            String salesOrderNoLineNum = analysisForecastBizDTO.getSalesOrderNo() + "-" + analysisForecastBizDTO.getLineNum();
            if (salesOrderNoLineNumSet.contains(salesOrderNoLineNum)){
                AssertUtils.isTrue(Boolean.TRUE,"OC-01-001-01-15-016","预报订单单号和行号");
            }else {
                salesOrderNoLineNumSet.add(salesOrderNoLineNum);
            }
        });
    }

    private static void confirmParamsEnumDictCheck(ConfirmReplyRequest confirmReplyRequest) {
        forecastOrderAbility.dictionaryVerifyAdapter.verifyDictionary(DictionaryTypeEnum.FROM_SYS.getCode(), confirmReplyRequest.getFromSys(), new FunctionException("OC-01-001-01-15-006", "来源系统编码"));
        forecastOrderAbility.dictionaryVerifyAdapter.verifyDictionary(DictionaryTypeEnum.BUSINESS_TYPE.getCode(), confirmReplyRequest.getBusinessType(), new FunctionException("OC-01-001-01-15-006", "业态编码"));
        forecastOrderAbility.dictionaryVerifyAdapter.verifyDictionary(DictionaryTypeEnum.BUSINESS_UNIT_CODE.getCode(), confirmReplyRequest.getBusinessUnitCode(), new FunctionException("OC-01-001-01-15-006", "业务单元编码"));
    }

    private static void confirmParamsLengthCheck(ConfirmReplyRequest confirmReplyRequest) {
        AssertUtils.isNumber(confirmReplyRequest.getBusinessType(), true, "OC-01-001-01-15-006", "业态编码");

        // 必填校验
        AssertUtils.lengthCheck(confirmReplyRequest.getFromSys(), 50, true, "OC-01-001-01-15-004", "来源系统编码");
        AssertUtils.lengthCheck(confirmReplyRequest.getBusinessType(), 10, true, "OC-01-001-01-15-004", "业态编码");
        AssertUtils.lengthCheck(confirmReplyRequest.getModifierCode(), 50, true, "OC-01-001-01-15-004", "修改人工号");
        AssertUtils.lengthCheck(confirmReplyRequest.getBusinessUnitCode(), 50, true, "OC-01-001-01-15-004", "业务单元编码");
        AssertUtils.isTrue(confirmReplyRequest.getConfirmReplyList().size() > 100, "OC-01-001-01-15-004", "预报订单明细行集合", confirmReplyRequest.getConfirmReplyList().size(), 100);
        confirmReplyRequest.getConfirmReplyList().forEach(analysisForecastBizDTO -> {
            AssertUtils.lengthCheck(analysisForecastBizDTO.getSalesOrderNo(), 50, true, "OC-01-001-01-15-004", "预报订单单号");
            AssertUtils.lengthCheck(analysisForecastBizDTO.getLineNum(), 10, true, "OC-01-001-01-15-004", "预报订单行号");
            AssertUtils.isTrue(new BigDecimal(INTEGER_MAX_VALUE).compareTo(new BigDecimal(analysisForecastBizDTO.getLineNum())) < 0, "OC-01-001-01-15-009", "预报订单行号", "最大2147483647");
            AssertUtils.isTrue(BigDecimal.ZERO.compareTo(new BigDecimal(analysisForecastBizDTO.getLineNum())) >= 0, "OC-01-001-01-15-009", "预报订单行号", "不允许为零或负数");
        });
    }

    private static void confirmParamsNotBlankCheck(ConfirmReplyRequest confirmReplyRequest) {
        AssertUtils.isTrue(Objects.isNull(confirmReplyRequest), "OC-01-001-01-15-001", "单据列表");
        AssertUtils.isTrue(StringUtils.isBlank(confirmReplyRequest.getFromSys()), "OC-01-001-01-15-001", "来源系统编码");
        AssertUtils.isTrue(StringUtils.isBlank(confirmReplyRequest.getBusinessType()), "OC-01-001-01-15-001", "业态编码");
        AssertUtils.isTrue(StringUtils.isBlank(confirmReplyRequest.getModifierCode()), "OC-01-001-01-15-001", "修改人工号");
        AssertUtils.isTrue(StringUtils.isBlank(confirmReplyRequest.getBusinessUnitCode()), "OC-01-001-01-15-001", "业务单元编码");
        AssertUtils.isTrue(CollectionUtils.isEmpty(confirmReplyRequest.getConfirmReplyList()), "OC-01-001-01-15-001", "预报订单明细行");
        AssertUtils.isTrue(confirmReplyRequest.getConfirmReplyList().size() > 100, "OC-01-001-01-15-004", "createForecastOrderDTOS", confirmReplyRequest.getConfirmReplyList().size(), "100");
        confirmReplyRequest.getConfirmReplyList().forEach(analysisForecastBizDTO -> {
                    AssertUtils.isTrue(StringUtils.isBlank(analysisForecastBizDTO.getSalesOrderNo()), "OC-01-001-01-15-001", "预报订单单号");
                    AssertUtils.isTrue(StringUtils.isBlank(analysisForecastBizDTO.getLineNum()), "OC-01-001-01-15-001", "预报订单行号");
                }
        );
    }

    public static void modifyParamsInvalid(ModifyForecastRequest modifyForecastRequest) {
        //should throw exception
        // 空值校验
        modifyParamsNotBlankCheck(modifyForecastRequest);

        // 长度校验
        modifyParamsLengthCheck(modifyForecastRequest);

        // 枚举校验（合法性校验）
//        modifyParamsEnumDictCheck(modifyForecastRequest);
    }

    public static void modifyParamsEnumDictCheck(ForecastOrderBO forecastOrderBO) {

        forecastOrderAbility.dictionaryVerifyAdapter.verifyDictionary(DictionaryTypeEnum.FROM_SYS.getCode(), forecastOrderBO.getFromSys(), new FunctionException("OC-01-001-01-15-006", "来源系统编码"));
        List<DictionaryResponse> businessTypeResponseList = forecastOrderAbility.baseDataServiceAdapter.queryDictionarys(DictionaryTypeEnum.BUSINESS_TYPE.getCode());
        Map<String, DictionaryResponse> businessTypeMap = businessTypeResponseList.stream().collect(Collectors.toMap(DictionaryResponse::getCode, Function.identity(), (k1, k2) -> k2));
        DictionaryResponse dictionaryResponse = businessTypeMap.get(forecastOrderBO.getBusinessType());
        if (dictionaryResponse == null) {
            throw new FunctionException("OC-01-001-01-15-006", "业态编码");
        }
        // 覆盖用户传入数据
        forecastOrderBO.setBusinessTypeName(dictionaryResponse.getName());

        if (StringUtils.isNotBlank(forecastOrderBO.getOrderTypeCode())) {
            ForecastOrderTypeEnum forecastOrderTypeEnum = ForecastOrderTypeEnum.of(forecastOrderBO.getOrderTypeCode());
            Optional.ofNullable(forecastOrderTypeEnum).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "订单类型编码"));
            //活动订单校验
            boolean isReplenishment = forecastOrderBO.getOrderTypeCode().equals(ForecastOrderTypeEnum.REPLENISH_FORECAST.getCode());

            if (isReplenishment) {
                forecastOrderBO.getForecastOrderLineBOList().forEach(detail -> {
                    AssertUtils.isTrue(StringUtils.isBlank(detail.getActivityCode()), "OC-01-001-01-15-001", "补货活动编码");
                    AssertUtils.isTrue(StringUtils.isBlank(detail.getActivityNumber()), "OC-01-001-01-15-001", "补货活动项目号");
                });
            } else {
                forecastOrderBO.getForecastOrderLineBOList().forEach(detail -> {
                    AssertUtils.isTrue(StringUtils.isNotBlank(detail.getActivityCode()), "OC-01-001-01-15-008", "补货活动编码");
                    AssertUtils.isTrue(StringUtils.isNotBlank(detail.getActivityNumber()), "OC-01-001-01-15-008", "补货活动项目号");
                });
            }
            // 覆盖用户传入数据
            forecastOrderBO.setOrderTypeName(forecastOrderTypeEnum.getDesc());
            forecastOrderBO.setOrderType(forecastOrderTypeEnum.getValue());
        }

        if (StringUtils.isNotBlank(forecastOrderBO.getTransportModeCode())) {
            SalesOrderDeliveryMethodEnum salesOrderDeliveryMethodEnum = SalesOrderDeliveryMethodEnum.of(forecastOrderBO.getTransportModeCode());
            Optional.ofNullable(salesOrderDeliveryMethodEnum).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "配送方式编码"));
            // 覆盖用户传入数据
            forecastOrderBO.setTransportModeName(salesOrderDeliveryMethodEnum.getDesc());
        }

        if (StringUtils.isNotBlank(forecastOrderBO.getCreateReasonCode())) {
            SalesOrderReasonEnum salesOrderReasonEnum = SalesOrderReasonEnum.of(forecastOrderBO.getCreateReasonCode());
            Optional.ofNullable(salesOrderReasonEnum).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "订单原因编码"));
            forecastOrderBO.setCreateReasonName(salesOrderReasonEnum.getDesc());
        }

        // 校验扩展字段是否为JSON格式
        AssertUtils.isTrue(!JsonUtils.isValidJson(forecastOrderBO.getBizFeatures()), "OC-00-001-01-15-001", "扩展字段");

        for (ForecastOrderLineBO p : forecastOrderBO.getForecastOrderLineBOList()) {
            if (StringUtils.isNotBlank(p.getSaleUnitCode())) {
                StockUnitEnum salesUnit = StockUnitEnum.of(p.getSaleUnitCode());
                Optional.ofNullable(salesUnit).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "销售单位编码"));
                // 覆盖用户传入数据
                p.setSaleUnitName(salesUnit.getDesc());

                AssertUtils.isTrue(!JsonUtils.isValidJson(p.getBizFeatures()), "OC-00-001-01-15-001", "扩展字段");

            }
        }
        if(StringUtils.isNotBlank(forecastOrderBO.getOrderCategoryCode())){
            OrderCategoryEnum orderCategoryEnum = OrderCategoryEnum.of(forecastOrderBO.getOrderCategoryCode());
            Optional.ofNullable(orderCategoryEnum).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "订单类别编码"));
        }
        // F-B一盘货销售预报订单
        if (StringUtils.isNotBlank(forecastOrderBO.getFulfillmentTypeCode())) {
            if(OrderCategoryEnum.GROUP.getCode().equals(forecastOrderBO.getOrderCategoryCode())){
                List<DictionaryResponse> fulfillmentTypeList = forecastOrderAbility.baseDataServiceAdapter.queryDictionarys(DictionaryTypeEnum.GROUP_SALE_FULFILLMENT_TYPE.getCode());
                Map<String, DictionaryResponse> fulfillmentTypeMap = fulfillmentTypeList.stream().collect(Collectors.toMap(DictionaryResponse::getCode, Function.identity(), (k1, k2) -> k2));
                AssertUtils.isTrue(fulfillmentTypeMap.get(forecastOrderBO.getFulfillmentTypeCode()) == null, "OC-01-001-01-15-006", "履约方式编码");
            }else if(OrderCategoryEnum.COMPANY.getCode().equals(forecastOrderBO.getOrderCategoryCode())){
                //履约方式
                List<DictionaryResponse> fulfillmentTypeList = forecastOrderAbility.baseDataServiceAdapter.queryDictionarys(DictionaryTypeEnum.COMPANY_SALE_FULFILLMENT_TYPE.getCode());
                Map<String, DictionaryResponse> fulfillmentTypeMap = fulfillmentTypeList.stream().collect(Collectors.toMap(DictionaryResponse::getCode, Function.identity(), (k1, k2) -> k2));
                AssertUtils.isTrue(fulfillmentTypeMap.get(forecastOrderBO.getFulfillmentTypeCode()) == null, "OC-01-001-01-15-006", "履约方式编码");
            }
        }
        if (StringUtils.isNotBlank(forecastOrderBO.getFulfillmentPriority())) {
            AssertUtils.isTrue(FulfillmentPriorityEnum.of(forecastOrderBO.getFulfillmentPriority()) == null, "OC-01-001-01-15-006", "履约优先级");
        }
        if (StringUtils.isNotBlank(forecastOrderBO.getTransportModeCode())) {
            AssertUtils.isTrue(SalesOrderDeliveryMethodEnum.of(forecastOrderBO.getTransportModeCode()) == null, "OC-01-001-01-15-006", "配送方式");
        }
        if (StringUtils.isNotBlank(forecastOrderBO.getOrderTag())) {
            List<String> tagList = Lists.newArrayList(forecastOrderBO.getOrderTag().split(","));
            /*if (tagList.size() != tagList.stream().distinct().count()) {
                throw new FunctionException("OC-01-001-01-15-016", "订单标签");
            }*/
            List<String> checkList = tagList.stream().filter(x -> !OrderBizTagEnum.codes().contains(x)).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(checkList)) {
                throw new FunctionException("OC-01-001-01-15-006", "订单标签");
            }
        }
    }

    private static void modifyParamsLengthCheck(ModifyForecastRequest modifyForecastRequest) {

        AssertUtils.isWholeNumber(modifyForecastRequest.getBusinessType(), true, "OC-01-001-01-15-006", "业态编码");
        // 必填校验
        AssertUtils.lengthCheck(modifyForecastRequest.getFromSys(), 50, true, "OC-01-001-01-15-004", "来源系统编码");
        AssertUtils.lengthCheck(modifyForecastRequest.getBusinessType(), 10, true, "OC-01-001-01-15-004", "业态编码");
        AssertUtils.lengthCheck(modifyForecastRequest.getBusinessUnitCode(), 50, true, "OC-01-001-01-15-004", "业务单元编码");
        ModifyForecastDTO forecastOrderBO = modifyForecastRequest.getModifyForecastDTO();
        AssertUtils.lengthCheck(forecastOrderBO.getSalesOrderNo(), 50, true, "OC-01-001-01-15-004", "预报订单单号");
        AssertUtils.lengthCheck(forecastOrderBO.getModifierCode(), 50, true, "OC-01-001-01-15-004", "修改人工号");
        AssertUtils.lengthCheck(forecastOrderBO.getSourceOrderNo(), 50, true, "OC-01-001-01-15-004", "来源单号");
        AssertUtils.isTrue(forecastOrderBO.getModifyForecastDetailDTOS().size() > 100, "OC-01-001-01-15-004", "单据明细", forecastOrderBO.getModifyForecastDetailDTOS().size(), 100);
        // 非必填校验，若填写需保证成对存在
        AssertUtils.lengthCheck(forecastOrderBO.getBusinessChannel(), 50, false, "OC-01-001-01-15-004", "业务渠道编码");
//        AssertUtils.lengthCheck(forecastOrderBO.getModifierName(), 50, false, "OC-01-001-01-15-004", "修改人姓名");
        AssertUtils.lengthCheck(forecastOrderBO.getSalesOrderType(), 10, false, "OC-01-001-01-15-004", "订单类型编码");
        AssertUtils.lengthCheck(forecastOrderBO.getCustomerCode(), 50, false, "OC-01-001-01-15-004", "客户售达方编码");
        AssertUtils.lengthCheck(forecastOrderBO.getShipToPartyCode(), 50, false, "OC-01-001-01-15-004", "客户送达方编码");
        AssertUtils.lengthCheck(forecastOrderBO.getProductGroupCode(), 50, false, "OC-01-001-01-15-004", "产品组编码");
        AssertUtils.lengthCheck(forecastOrderBO.getSalesChannelCode(), 50, false, "OC-01-001-01-15-004", "销售渠道编码");
        AssertUtils.lengthCheck(forecastOrderBO.getOrderReasonCode(), 50, false, "OC-01-001-01-15-004", "订单原因编码");
        AssertUtils.lengthCheck(forecastOrderBO.getDeliveryMethodCode(), 50, false, "OC-01-001-01-15-004", "配送方式编码");
        AssertUtils.lengthCheck(forecastOrderBO.getExpectedDeliveryDate(), 10, false, "OC-01-001-01-15-004", "期望交货日期");
        AssertUtils.lengthCheck(forecastOrderBO.getVaildBeginDate(), 10, false, "OC-01-001-01-15-004", "生效日期");
        AssertUtils.lengthCheck(forecastOrderBO.getSalesDepartmentCode(), 50, false, "OC-01-001-01-15-004", "销售部门编码");
        AssertUtils.lengthCheck(forecastOrderBO.getCustomerPurchaseDate(), 10, false, "OC-01-001-01-15-004", "客户采购订单日期");
        AssertUtils.lengthCheck(forecastOrderBO.getSalesOrganizationCode(), 50, false, "OC-01-001-01-15-004", "销售组织编码");
        AssertUtils.lengthCheck(forecastOrderBO.getCustomerPurchaseOrderNo(), 50, false, "OC-01-001-01-15-004", "客户采购订单号");
        AssertUtils.lengthCheck(forecastOrderBO.getFulfillmentTypeCode(), 50, false, "OC-01-001-01-15-004", "履约方式编码");
        AssertUtils.lengthCheck(forecastOrderBO.getFulfillmentPriority(), 10, false, "OC-01-001-01-15-004", "履约优先级");
        AssertUtils.lengthCheck(forecastOrderBO.getAutoFulfillment(), 50, false, "OC-01-001-01-15-004", "是否自动创建履约单");
        AssertUtils.lengthCheck(forecastOrderBO.getNote(), 255, false, "OC-01-001-01-15-004", "订单头备注");
        AssertUtils.lengthCheck(forecastOrderBO.getBizFeatures(), 1024, false, "OC-01-001-01-15-004", "扩展字段");
        if (CollectionUtils.isNotEmpty(forecastOrderBO.getOrderBizTag())) {
            AssertUtils.isTrue(forecastOrderBO.getOrderBizTag().size() > 10, "OC-01-001-01-15-004", "订单标签集合", forecastOrderBO.getOrderBizTag().size(), 10);
            Set<String> orderBizTagSet = new HashSet<>();
            forecastOrderBO.getOrderBizTag().forEach(x -> {
                if (!orderBizTagSet.add(x)) {
                    throw new FunctionException("OC-01-001-01-15-016", "订单标签集合");
                }
                AssertUtils.lengthCheck(x, 50, false, "OC-01-001-01-15-004", "订单标签");
            });
            forecastOrderBO.setOrderBizTag(Lists.newArrayList(orderBizTagSet));
        }
        // 日期格式校验
        if (StringUtils.isNotBlank(forecastOrderBO.getExpectedDeliveryDate())) {
            AssertUtils.isTrue(!DateUtils.isDateFormat(forecastOrderBO.getExpectedDeliveryDate()), "OC-00-001-01-15-001", "期望交货日期");
            if(!OrderCategoryEnum.GROUP.getCode().equals(forecastOrderBO.getOrderCategoryCode())) {
                AssertUtils.isTrue(DateUtils.formatDate(new Date()).compareTo(forecastOrderBO.getExpectedDeliveryDate()) > 0, "OC-01-001-01-16-021");
            }
        }
        if (StringUtils.isNotBlank(forecastOrderBO.getCustomerPurchaseDate())) {
            AssertUtils.isTrue(!DateUtils.isDateFormat(forecastOrderBO.getCustomerPurchaseDate()), "OC-01-001-01-15-006", "客户采购订单日期");
        }
        if (StringUtils.isNotBlank(forecastOrderBO.getVaildBeginDate())) {
            AssertUtils.isTrue(!DateUtils.isDateFormat(forecastOrderBO.getVaildBeginDate()), "OC-01-001-01-15-006", "生效日期");
        }
        forecastOrderBO.getModifyForecastDetailDTOS().forEach(detail -> {
            if (StringUtils.isBlank(detail.getLineNum())) {
                AssertUtils.isWholeNumber(detail.getProductQuantity(), true, "OC-01-001-01-15-006", "预报数量");
                AssertUtils.lengthCheck(detail.getProductQuantity(), 10, true, "OC-01-001-01-15-004", "预报数量");
                AssertUtils.lengthCheck(detail.getProductCode(), 50, true, "OC-01-001-01-15-004", "物料编码");
                AssertUtils.lengthCheck(detail.getSaleUnitCode(), 50, true, "OC-01-001-01-15-004", "销售单位编码");
            }
            AssertUtils.isWholeNumber(detail.getProductQuantity(), false, "OC-01-001-01-15-006", "预报数量");
            AssertUtils.lengthCheck(detail.getLineNum(), 10, false, "OC-01-001-01-15-004", "行号");
            AssertUtils.lengthCheck(detail.getProductQuantity(), 10, false, "OC-01-001-01-15-004", "预报数量");
            AssertUtils.lengthCheck(detail.getProductCode(), 50, false, "OC-01-001-01-15-004", "物料编码");
            AssertUtils.lengthCheck(detail.getActivityId(), 50, false, "OC-01-001-01-15-004", "活动编码");
            AssertUtils.lengthCheck(detail.getNote(), 255, false, "OC-01-001-01-15-004", "订单行备注");
            AssertUtils.lengthCheck(detail.getSaleUnitCode(), 50, false, "OC-01-001-01-15-004", "销售单位编码");
            AssertUtils.lengthCheck(detail.getSourceLineNum(), 10, true, "OC-01-001-01-15-004", "来源单行号");
            AssertUtils.lengthCheck(detail.getActivityLineNo(), 50, false, "OC-01-001-01-15-004", "活动项目号");
            AssertUtils.lengthCheck(detail.getShippingWarehouseCode(), 50, false, "OC-01-001-01-15-004", "发货仓编码");
            AssertUtils.lengthCheck(detail.getDeliveryFactoryCode(), 50, false, "OC-01-001-01-15-004", "发货工厂编码");
            AssertUtils.lengthCheck(detail.getStockCode(), 50, false, "OC-01-001-01-15-004", "发货库存地点编码");
            AssertUtils.lengthCheck(detail.getProductionDate(), 10, false, "OC-01-001-01-15-004", "生产日期");
            AssertUtils.lengthCheck(detail.getSourceUnit(), 50, false, "OC-01-001-01-15-004", "来单单位");
            AssertUtils.lengthCheck(detail.getSourceQuantity(), 10, false, "OC-01-001-01-15-004", "来单数量");
            AssertUtils.isWholeNumber(detail.getSourceQuantity(), false, "OC-01-001-01-15-006", "来单数量");
            AssertUtils.lengthCheck(detail.getSourcePrice(), 10, false, "OC-01-001-01-15-004", "来单价格");
            AssertUtils.isAmountNumber(detail.getSourcePrice(), false, "OC-01-001-01-15-006", "来单价格");
            AssertUtils.moneyCheck(detail.getSourcePrice(), false, "OC-01-001-01-15-005", "来单价格");
            if (StringUtils.isNotBlank(detail.getProductQuantity())) {
                AssertUtils.isTrue(new BigDecimal(INTEGER_MAX_VALUE).compareTo(new BigDecimal(detail.getProductQuantity())) < 0, "OC-01-001-01-15-009", "预报数量", "最大2147483647");
                AssertUtils.isTrue(BigDecimal.ZERO.compareTo(new BigDecimal(detail.getProductQuantity())) >= 0, "OC-01-001-01-15-009", "预报数量", "不允许为零或负数");
            }
            if (StringUtils.isNotBlank(detail.getSourceQuantity())) {
                AssertUtils.isTrue(new BigDecimal(INTEGER_MAX_VALUE).compareTo(new BigDecimal(detail.getSourceQuantity())) < 0, "OC-01-001-01-15-009", "来单数量", "最大2147483647");
                AssertUtils.isTrue(BigDecimal.ZERO.compareTo(new BigDecimal(detail.getSourceQuantity())) > 0, "OC-01-001-01-15-009", "来单数量", "不允许为负数");
            }
            if (StringUtils.isNotBlank(detail.getSourcePrice())) {
                AssertUtils.isTrue(new BigDecimal(INTEGER_MAX_VALUE).compareTo(new BigDecimal(detail.getSourcePrice())) < 0, "OC-01-001-01-15-009", "来单价格", "最大2147483647");
                AssertUtils.isTrue(BigDecimal.ZERO.compareTo(new BigDecimal(detail.getSourcePrice())) > 0, "OC-01-001-01-15-009", "来单价格", "不允许为负数");
            }
            if (StringUtils.isNotBlank(detail.getProductionDate())) {
                AssertUtils.isTrue(!DateUtils.isDateFormat(detail.getProductionDate()), "OC-01-001-01-15-006", "生产日期");
            }
            AssertUtils.lengthCheck(detail.getBizFeatures(), 1024, false, "OC-01-001-01-15-004", "子单扩展字段");
        });
    }

    private static void modifyParamsNotBlankCheck(ModifyForecastRequest modifyForecastRequest) {
        AssertUtils.isTrue(Objects.isNull(modifyForecastRequest), "OC-01-001-01-15-001", "请求参数");

        AssertUtils.isTrue(StringUtils.isBlank(modifyForecastRequest.getFromSys()), "OC-01-001-01-15-001", "来源系统编码");
        AssertUtils.isTrue(StringUtils.isBlank(modifyForecastRequest.getBusinessType()), "OC-01-001-01-15-001", "业态编码");
        AssertUtils.isTrue(StringUtils.isBlank(modifyForecastRequest.getBusinessUnitCode()), "OC-01-001-01-15-001", "业务单元编码");
        ModifyForecastDTO forecastOrderBO = modifyForecastRequest.getModifyForecastDTO();
        AssertUtils.isTrue(Objects.isNull(forecastOrderBO), "OC-01-001-01-15-001", "预报订单");

        AssertUtils.isTrue(StringUtils.isBlank(forecastOrderBO.getSourceOrderNo()), "OC-01-001-01-15-001", "来源单号");
        AssertUtils.isTrue(StringUtils.isBlank(forecastOrderBO.getSalesOrderNo()), "OC-01-001-01-15-001", "预报订单单号");
        AssertUtils.isTrue(StringUtils.isBlank(forecastOrderBO.getModifierCode()), "OC-01-001-01-15-001", "修改人工号");

        AssertUtils.isTrue(CollectionUtils.isEmpty(forecastOrderBO.getModifyForecastDetailDTOS()), "OC-01-001-01-15-001", "单据明细");
        forecastOrderBO.getModifyForecastDetailDTOS().forEach(detail -> {
            AssertUtils.isTrue(StringUtils.isBlank(detail.getSourceLineNum()), "OC-01-001-01-15-001", "来源单行号");

            if (StringUtils.isBlank(detail.getLineNum())) {
                // AssertUtils.isTrue(StringUtils.isBlank(detail.getProductQuantity()), "OC-01-001-01-15-001", "预报数量");
                AssertUtils.isTrue(StringUtils.isBlank(detail.getProductCode()), "OC-01-001-01-15-001", "物料编码");
                AssertUtils.isTrue(StringUtils.isBlank(detail.getSaleUnitCode()), "OC-01-001-01-15-001", "销售单位编码");
                AssertUtils.isTrue(StringUtils.isBlank(detail.getProductQuantity()), "OC-01-001-01-15-001", "预报数量");

                if (StringUtils.isBlank(detail.getShippingWarehouseCode())) {
                    if (StringUtils.isBlank(detail.getDeliveryFactoryCode()) && StringUtils.isBlank(detail.getStockCode())) {
                        throw new FunctionException("OC-01-001-01-15-010", "发货仓编码、发货工厂编码、发货库存地点编码");
                    }
                    AssertUtils.isTrue(StringUtils.isBlank(detail.getDeliveryFactoryCode()), "OC-01-001-01-15-001", "发货工厂编码");
                    AssertUtils.isTrue(StringUtils.isBlank(detail.getStockCode()), "OC-01-001-01-15-001", "发货库存地点编码");
                }
            }
            if(OrderCategoryEnum.GROUP.getCode().equals(forecastOrderBO.getOrderCategoryCode())) {
                List<ModifyForecastDetailDTO> detailDTOList = forecastOrderBO.getModifyForecastDetailDTOS().stream()
                        .filter(x -> StringUtils.isNotBlank(x.getLineNum())).collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(detailDTOList)) {
                    Map<String, List<ModifyForecastDetailDTO>> lineMap = detailDTOList.stream().collect(Collectors.groupingBy(ModifyForecastDetailDTO::getLineNum));
                    List<ModifyForecastDetailDTO> errorDTOList = forecastOrderBO.getModifyForecastDetailDTOS().stream()
                            .filter(x -> StringUtils.isNotBlank(x.getLineNum()) && lineMap.get(x.getLineNum()).size() > 1).collect(Collectors.toList());
                    if (CollectionUtils.isNotEmpty(errorDTOList)) {
                        throw new FunctionException("OC-01-001-01-15-016", "预报单行号");
                    }
                }

                // 三个都不传
//                if (StringUtils.isBlank(detail.getShippingWarehouseCode())
//                        && StringUtils.isBlank(detail.getDeliveryFactoryCode())
//                        && StringUtils.isBlank(detail.getStockCode())) {
//                    throw new FunctionException("OC-01-001-01-15-010", "发货仓编码、发货工厂编码、发货库存地点编码");
//                }
//                // 发货仓不传的情况下，"发货工厂编码","发货库存地点编码"必须成对出现
//                if (StringUtils.isBlank(detail.getShippingWarehouseCode())) {
//                    if ((StringUtils.isBlank(detail.getDeliveryFactoryCode()) && StringUtils.isNotBlank(detail.getStockCode()))
//                            || (StringUtils.isNotBlank(detail.getDeliveryFactoryCode()) && StringUtils.isBlank(detail.getStockCode()))) {
//                        throw new FunctionException("OC-01-001-01-15-015", "发货工厂编码","发货库存地点编码");
//                    }
//                }
            }
        });

        // 明细行号不能重复
        List<String> sameSourceLines = forecastOrderBO.getModifyForecastDetailDTOS()
                .stream()
                .map(ModifyForecastDetailDTO::getSourceLineNum)
                .filter(StringUtils::isNotBlank)
                .collect(Collectors.toMap(p -> p, p -> 1, Integer::sum))
                .entrySet().stream().filter(entry -> entry.getValue() > 1)
                .map(Map.Entry::getKey)
                .collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(sameSourceLines)) {
            throw new FunctionException("OC-01-001-01-15-002", "来源单行号", StringUtils.join(sameSourceLines, ","));
        }

    }

    public static void submitParamsInvalid(ForecastBizBatchSubmitRequest request) {
        // 空值校验
        submitParamsNotBlankCheck(request);
        // 长度校验
        submitParamsLengthCheck(request);
        // 枚举校验（合法性校验）
        submitParamsEnumDictCheck(request);
    }

    private static void submitParamsEnumDictCheck(ForecastBizBatchSubmitRequest request) {
        forecastOrderAbility.dictionaryVerifyAdapter.verifyDictionary(DictionaryTypeEnum.FROM_SYS.getCode(), request.getFromSys(), new FunctionException("OC-01-001-01-15-006", "来源系统编码"));
        forecastOrderAbility.dictionaryVerifyAdapter.verifyDictionary(DictionaryTypeEnum.BUSINESS_TYPE.getCode(), request.getBusinessType(), new FunctionException("OC-01-001-01-15-006", "业态编码"));
        forecastOrderAbility.dictionaryVerifyAdapter.verifyDictionary(DictionaryTypeEnum.BUSINESS_UNIT_CODE.getCode(), request.getBusinessUnitCode(), new FunctionException("OC-01-001-01-15-006", "业务单元编码"));
    }

    private static void submitParamsLengthCheck(ForecastBizBatchSubmitRequest request) {
        AssertUtils.lengthCheck(request.getFromSys(), 50, true, "OC-01-001-01-15-004", "来源系统编码");
        AssertUtils.lengthCheck(request.getBusinessType(), 10, true, "OC-01-001-01-15-004", "业态编码");
        AssertUtils.lengthCheck(request.getBusinessUnitCode(), 50, true, "OC-01-001-01-15-004", "业务单元编码");
        AssertUtils.lengthCheck(request.getModifierCode(), 50, true, "OC-01-001-01-15-004", "修改人工号");

        AssertUtils.isNumber(request.getBusinessType(), true, "OC-01-001-01-15-006", "业态编码");

        request.getSubmitForecastDTOS().forEach(submitForecastDTO -> AssertUtils.lengthCheck(submitForecastDTO.getSalesOrderNo(), 50, true, "OC-01-001-01-15-004", "预报订单单号"));

        // 重复值校验
        Set<String> sameLineNum = new HashSet<>();
        request.getSubmitForecastDTOS().forEach(detail -> {
            if (sameLineNum.contains(detail.getSalesOrderNo())) {
                throw new FunctionException("OC-01-001-01-15-002", "单据列表", detail.getSalesOrderNo());
            } else {
                sameLineNum.add(detail.getSalesOrderNo());
            }
        });
    }

    private static void submitParamsNotBlankCheck(ForecastBizBatchSubmitRequest request) {
        AssertUtils.isTrue(StringUtils.isBlank(request.getFromSys()), "OC-01-001-01-15-001", "来源系统编码");
        AssertUtils.isTrue(StringUtils.isBlank(request.getBusinessType()), "OC-01-001-01-15-001", "业态编码");
        AssertUtils.isTrue(StringUtils.isBlank(request.getBusinessUnitCode()), "OC-01-001-01-15-001", "业务单元编码");
        AssertUtils.isTrue(StringUtils.isBlank(request.getModifierCode()), "OC-01-001-01-15-001", "修改人工号");

        AssertUtils.isTrue(CollectionUtils.isEmpty(request.getSubmitForecastDTOS()), "OC-01-001-01-15-001", "单据列表");
        AssertUtils.isTrue(request.getSubmitForecastDTOS().size() > 100, "OC-01-001-01-15-004", "单据列表", String.valueOf(request.getSubmitForecastDTOS().size()), "100");

        request.getSubmitForecastDTOS().forEach(submitForecastDTO -> {
            AssertUtils.isTrue(StringUtils.isBlank(submitForecastDTO.getSalesOrderNo()), "OC-01-001-01-15-001", "预报订单单号");
        });
    }

    public static void paramsNotBlankCheck(List<ForecastOrderEntity> forecastOrderEntities) {
        AssertUtils.isTrue(CollectionUtils.isEmpty(forecastOrderEntities), "OC-01-001-01-15-001", "单据列表");
        AssertUtils.isTrue(forecastOrderEntities.size() > 100, "OC-01-001-01-15-004", "单据列表", String.valueOf(forecastOrderEntities.size()), "100");

        forecastOrderEntities.forEach(forecastOrderEntity -> {
            ForecastOrderBO forecastOrderBO = forecastOrderEntity.getForecastOrderBO();
            if (StringUtils.isBlank(forecastOrderBO.getSubmitFlag())) {
                // 设置是否提交默认值为true
                forecastOrderBO.setSubmitFlag("true");
            }
            if (StringUtils.isBlank(forecastOrderBO.getAutoFulfillment())) {
                // 设置是否自动履约默认值为true
                forecastOrderBO.setAutoFulfillment("true");
            }

            // 必填校验
            AssertUtils.isTrue(StringUtils.isBlank(forecastOrderBO.getReqNo()), "OC-01-001-01-15-001", "请求编码");
            AssertUtils.isTrue(StringUtils.isBlank(forecastOrderBO.getFromSys()), "OC-01-001-01-15-001", "来源系统编码");
            AssertUtils.isTrue(StringUtils.isBlank(forecastOrderBO.getCreatorCode()), "OC-01-001-01-15-001", "创建人工号");
            AssertUtils.isTrue(StringUtils.isBlank(forecastOrderBO.getChannelCode()), "OC-01-001-01-15-001", "业务渠道编码");
            AssertUtils.isTrue(StringUtils.isBlank(forecastOrderBO.getBusinessType()), "OC-01-001-01-15-001", "业态编码");
            AssertUtils.isTrue(StringUtils.isBlank(forecastOrderBO.getSourceOrderNo()), "OC-01-001-01-15-001", "来源单号");
//            AssertUtils.isTrue(StringUtils.isBlank(forecastOrderBO.getOrderCategoryCode()), "OC-01-001-01-15-001", "订单类别编码");
            AssertUtils.isTrue(StringUtils.isBlank(forecastOrderBO.getOrderTypeCode()), "OC-01-001-01-15-001", "订单类型编码");
            AssertUtils.isTrue(StringUtils.isBlank(forecastOrderBO.getCustomerExpectTDate()), "OC-01-001-01-15-001", "期望交货日期");
            AssertUtils.isTrue(StringUtils.isBlank(forecastOrderBO.getSoldToPartyCode()), "OC-01-001-01-15-001", "客户售达方编码");
            AssertUtils.isTrue(StringUtils.isBlank(forecastOrderBO.getShipToPartyCode()), "OC-01-001-01-15-001", "客户送达方编码");
            AssertUtils.isTrue(StringUtils.isBlank(forecastOrderBO.getProductGroupCode()), "OC-01-001-01-15-001", "产品组编码");
            AssertUtils.isTrue(StringUtils.isBlank(forecastOrderBO.getBusinessUnitCode()), "OC-01-001-01-15-001", "业务单元编码");
            AssertUtils.isTrue(StringUtils.isBlank(forecastOrderBO.getTransportModeCode()), "OC-01-001-01-15-001", "配送方式编码");
            AssertUtils.isTrue(StringUtils.isBlank(forecastOrderBO.getSalesOrganizationCode()), "OC-01-001-01-15-001", "销售组织编码");
            AssertUtils.isTrue(CollectionUtils.isEmpty(forecastOrderBO.getForecastOrderLineBOList()), "OC-01-001-01-15-001", "单据明细");

            // 识别是否为集团预报订单
            boolean isGroup = forecastOrderBO.getOrderTypeCode().equals(ForecastOrderTypeEnum.GROUP_FORECAST.getCode());
            if (isGroup) {
                AssertUtils.isTrue(StringUtils.isBlank(forecastOrderBO.getOrderChannelCode()), "OC-01-001-01-15-001", "订单渠道编码");
                // 集团预报订单校验收货工厂编码和收货库存地点编码
                if (StringUtils.isNotBlank(forecastOrderBO.getReceiveFactoryCode()) || StringUtils.isNotBlank(forecastOrderBO.getReceiveStockCode())) {
                    AssertUtils.isTrue(StringUtils.isBlank(forecastOrderBO.getReceiveFactoryCode()), "OC-01-001-01-15-001", "收货工厂编码");
                    AssertUtils.isTrue(StringUtils.isBlank(forecastOrderBO.getReceiveStockCode()), "OC-01-001-01-15-001", "收货库存地点编码");
                }
            }

            forecastOrderBO.getForecastOrderLineBOList().forEach(detail -> {
                AssertUtils.isTrue(StringUtils.isBlank(detail.getQuantity()), "OC-01-001-01-15-001", "预报数量");
                AssertUtils.isTrue(StringUtils.isBlank(detail.getProductCode()), "OC-01-001-01-15-001", "物料编码");
                AssertUtils.isTrue(StringUtils.isBlank(detail.getSaleUnitCode()), "OC-01-001-01-15-001", "销售单位编码");
                AssertUtils.isTrue(StringUtils.isBlank(detail.getSourceLineNum()), "OC-01-001-01-15-001", "来源单行号");

                if (StringUtils.isBlank(detail.getShippingWarehouseCode())) {
                    if (StringUtils.isBlank(detail.getDeliveryFactoryCode()) && StringUtils.isBlank(detail.getStockCode())) {
                        throw new FunctionException("OC-01-001-01-15-010", "发货仓编码、发货工厂编码、发货库存地点编码");
                    }
                    AssertUtils.isTrue(StringUtils.isBlank(detail.getDeliveryFactoryCode()), "OC-01-001-01-15-001", "发货工厂编码");
                    AssertUtils.isTrue(StringUtils.isBlank(detail.getStockCode()), "OC-01-001-01-15-001", "发货库存地点编码");
                }
            });

            // 明细行号不能重复
            List<String> sameSourceLines = forecastOrderBO.getForecastOrderLineBOList().stream()
                    .map(ForecastOrderLineBO::getSourceLineNum)
                    .collect(Collectors.toMap(p -> p, p -> 1, Integer::sum))
                    .entrySet().stream().filter(entry -> entry.getValue() > 1)
                    .map(Map.Entry::getKey).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(sameSourceLines)) {
                throw new FunctionException("OC-01-001-01-15-002", "来源单行号", StringUtils.join(sameSourceLines, ","));
            }
        });

        // 重复数据校验 取出重复数据
        List<String> sameSourceOrderNos = forecastOrderEntities.stream()
                .map(ForecastOrderEntity::getForecastOrderBO)
                .map(ForecastOrderBO::getSourceOrderNo)
                .collect(Collectors.toMap(forecastOrderBO -> forecastOrderBO, forecastOrderBO -> 1, Integer::sum))
                .entrySet().stream().filter(entry -> entry.getValue() > 1)
                .map(Map.Entry::getKey).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(sameSourceOrderNos)) {
            throw new FunctionException("OC-01-001-01-15-002", "来源单号", StringUtils.join(sameSourceOrderNos, ","));
        }
    }

    public static void paramsLengthCheck(List<ForecastOrderEntity> forecastOrderEntities) {
        forecastOrderEntities.forEach(forecastOrderEntity -> {
            ForecastOrderBO forecastOrderBO = forecastOrderEntity.getForecastOrderBO();

            // 识别是否为集团预报订单
            boolean isGroup = forecastOrderBO.getOrderTypeCode().equals(ForecastOrderTypeEnum.GROUP_FORECAST.getCode());

            AssertUtils.isNumber(forecastOrderBO.getBusinessType(), true, "OC-01-001-01-15-006", "业态编码");

            // 长度校验
            AssertUtils.lengthCheck(forecastOrderBO.getReqNo(), 128, true, "OC-01-001-01-15-004", "请求编码");
            AssertUtils.lengthCheck(forecastOrderBO.getSubmitFlag(), 50, true, "OC-01-001-01-15-004", "是否提交");
            AssertUtils.lengthCheck(forecastOrderBO.getRemark(), 255, false, "OC-01-001-01-15-004", "订单头备注");
            AssertUtils.lengthCheck(forecastOrderBO.getFromSys(), 50, true, "OC-01-001-01-15-004", "来源系统编码");
            AssertUtils.lengthCheck(forecastOrderBO.getCreatorCode(), 50, true, "OC-01-001-01-15-004", "创建人工号");
            AssertUtils.lengthCheck(forecastOrderBO.getCreatorName(), 50, false, "OC-01-001-01-15-004", "创建人姓名");
            AssertUtils.lengthCheck(forecastOrderBO.getChannelCode(), 50, true, "OC-01-001-01-15-004", "业务渠道编码");
            AssertUtils.lengthCheck(forecastOrderBO.getChannelName(), 50, false, "OC-01-001-01-15-004", "业务渠道名称");
            AssertUtils.lengthCheck(forecastOrderBO.getBusinessType(), 10, true, "OC-01-001-01-15-004", "业态编码");
            AssertUtils.lengthCheck(forecastOrderBO.getOrderCategoryCode(), 50, false, "OC-01-001-01-15-004", "订单类别编码");
            AssertUtils.lengthCheck(forecastOrderBO.getOrderChannelCode(), 50, false, "OC-01-001-01-15-004", "订单渠道编码");
            AssertUtils.lengthCheck(forecastOrderBO.getOrderTypeCode(), 10, true, "OC-01-001-01-15-004", "订单类型编码");
            AssertUtils.lengthCheck(forecastOrderBO.getOrderTypeName(), 50, false, "OC-01-001-01-15-004", "订单类型名称");
            AssertUtils.lengthCheck(forecastOrderBO.getSourceOrderNo(), 50, true, "OC-01-001-01-15-004", "来源单号");
            AssertUtils.lengthCheck(forecastOrderBO.getSoldToPartyCode(), 50, true, "OC-01-001-01-15-004", "客户售达方编码");
            AssertUtils.lengthCheck(forecastOrderBO.getSoldToPartyName(), 50, false, "OC-01-001-01-15-004", "客户售达方名称");
            AssertUtils.lengthCheck(forecastOrderBO.getShipToPartyCode(), 50, true, "OC-01-001-01-15-004", "客户送达方编码");
            AssertUtils.lengthCheck(forecastOrderBO.getShipToPartyName(), 50, false, "OC-01-001-01-15-004", "客户送达方名称");
            AssertUtils.lengthCheck(forecastOrderBO.getProductGroupCode(), 50, true, "OC-01-001-01-15-004", "产品组编码");
            AssertUtils.lengthCheck(forecastOrderBO.getProductGroupName(), 50, false, "OC-01-001-01-15-004", "产品组名称");
            AssertUtils.lengthCheck(forecastOrderBO.getBusinessUnitCode(), 50, true, "OC-01-001-01-15-004", "业务单元编码");
            AssertUtils.lengthCheck(forecastOrderBO.getSalesChannelCode(), 50, false, "OC-01-001-01-15-004", "销售渠道编码");
            AssertUtils.lengthCheck(forecastOrderBO.getSalesChannelName(), 50, false, "OC-01-001-01-15-004", "销售渠道名称");
            AssertUtils.lengthCheck(forecastOrderBO.getCreateReasonCode(), 50, false, "OC-01-001-01-15-004", "订单原因编码");
            AssertUtils.lengthCheck(forecastOrderBO.getTransportModeCode(), 50, true, "OC-01-001-01-15-004", "配送方式编码");
            AssertUtils.lengthCheck(forecastOrderBO.getTransportModeName(), 50, false, "OC-01-001-01-15-004", "配送方式名称");
            AssertUtils.lengthCheck(forecastOrderBO.getSalesDepartmentCode(), 50, false, "OC-01-001-01-15-004", "销售部门编码");
            AssertUtils.lengthCheck(forecastOrderBO.getSalesDepartmentName(), 50, false, "OC-01-001-01-15-004", "销售部门名称");
            AssertUtils.lengthCheck(forecastOrderBO.getCustomerExpectTDate(), 10, true, "OC-01-001-01-15-004", "期望交货日期");
            AssertUtils.lengthCheck(forecastOrderBO.getCustomerPurchaseDate(), 10, false, "OC-01-001-01-15-004", "客户采购订单日期");
            AssertUtils.lengthCheck(forecastOrderBO.getSalesOrganizationCode(), 50, true, "OC-01-001-01-15-004", "销售组织编码");
            AssertUtils.lengthCheck(forecastOrderBO.getSalesOrganizationName(), 50, false, "OC-01-001-01-15-004", "销售组织名称");
            AssertUtils.lengthCheck(forecastOrderBO.getCustomerPurchaseOrderNo(), 50, false, "OC-01-001-01-15-004", "客户采购订单号");
            AssertUtils.lengthCheck(forecastOrderBO.getCustomerGrade(), 50, false, "OC-01-001-01-15-004", "客户等级");
            AssertUtils.lengthCheck(forecastOrderBO.getCustomerGroup(), 50, false, "OC-01-001-01-15-004", "客户组");
            // 日期格式校验
            AssertUtils.isTrue(!DateUtils.isDateFormat(forecastOrderBO.getCustomerExpectTDate()), "OC-00-001-01-15-001", "期望交货日期");
            // 集团销售预报订单不校验时间坐标
            if (!isGroup) {
                AssertUtils.isTrue(DateUtils.formatDate(new Date()).compareTo(forecastOrderBO.getCustomerExpectTDate()) > 0, "OC-01-001-01-16-021");
            }
            if (StringUtils.isNotBlank(forecastOrderBO.getCustomerPurchaseDate())) {
                AssertUtils.isTrue(!DateUtils.isDateFormat(forecastOrderBO.getCustomerPurchaseDate()), "OC-00-001-01-15-001", "客户采购订单日期");
            }
            // 校验扩展字段长度和格式
            AssertUtils.lengthCheck(forecastOrderBO.getBizFeatures(), 1024, false, "OC-01-001-01-15-004", "扩展字段");
            AssertUtils.isTrue(!JsonUtils.isValidJson(forecastOrderBO.getBizFeatures()), "OC-00-001-01-15-001", "扩展字段");

            // 集团销售预报订单校验字段
            AssertUtils.lengthCheck(forecastOrderBO.getFulfillmentTypeCode(), 50, false, "OC-01-001-01-15-004", "履约方式编码");
            AssertUtils.lengthCheck(forecastOrderBO.getFulfillmentPriority(), 10, false, "OC-01-001-01-15-004", "履约优先级");
//            AssertUtils.lengthCheck(forecastOrderBO.getOrderTag(), 50, false, "OC-01-001-01-15-004", "订单标签");

            //B-b分公司预报订单校验字段
            AssertUtils.lengthCheck(forecastOrderBO.getKaSystem(), 50, false, "OC-01-001-01-15-004", "KA系统");
            AssertUtils.lengthCheck(forecastOrderBO.getKaArea(), 50, false, "OC-01-001-01-15-004", "KA大区");
            AssertUtils.lengthCheck(forecastOrderBO.getReceiveWarehouseCode(), 50, false, "OC-01-001-01-15-004", "收货逻辑仓库编码");
            AssertUtils.lengthCheck(forecastOrderBO.getReceiveFactoryCode(), 50, false, "OC-01-001-01-15-004", "收货工厂编码");
            AssertUtils.lengthCheck(forecastOrderBO.getReceiveStockCode(), 50, false, "OC-01-001-01-15-004", "收货库存地点编码");
            AssertUtils.lengthCheck(forecastOrderBO.getAutoFulfillment(), 10, false, "OC-01-001-01-15-004", "是否自动创建履约单");
            AssertUtils.lengthCheck(forecastOrderBO.getSourceOrderDate(), 19, false, "OC-01-001-01-15-004", "来单时间");
            if(StringUtils.isNotBlank(forecastOrderBO.getSourceOrderDate())){
                AssertUtils.isTrue(!DateUtils.isDateTimeFormat(forecastOrderBO.getSourceOrderDate()), "OC-00-001-01-15-001", "来单时间");
            }
            List<ForecastOrderLineBO> detailList = forecastOrderBO.getForecastOrderLineBOList();
            AssertUtils.isTrue(detailList.size() > 100, "OC-01-001-01-15-004", "forecastOrderDetailDTOS", detailList.size(), "100");
            detailList.forEach(detail -> {
                AssertUtils.isWholeNumber(detail.getQuantity(), true, "OC-01-001-01-15-006", "预报数量");
                AssertUtils.lengthCheck(detail.getQuantity(), 10, true, "OC-01-001-01-15-004", "预报数量");
                AssertUtils.lengthCheck(detail.getProductCode(), 50, true, "OC-01-001-01-15-004", "物料编码");
                AssertUtils.lengthCheck(detail.getProductName(), 50, false, "OC-01-001-01-15-004", "物料名称");
                AssertUtils.lengthCheck(detail.getDetailRemark(), 255, false, "OC-01-001-01-15-004", "订单行备注");
                AssertUtils.lengthCheck(detail.getSaleUnitCode(), 50, true, "OC-01-001-01-15-004", "销售单位编码");
                AssertUtils.lengthCheck(detail.getSaleUnitName(), 50, false, "OC-01-001-01-15-004", "销售单位名称");
                AssertUtils.lengthCheck(detail.getActivityCode(), 50, false, "OC-01-001-01-15-004", "活动编码");
                AssertUtils.lengthCheck(detail.getSourceLineNum(), 10, true, "OC-01-001-01-15-004", "来源单行号");
                AssertUtils.lengthCheck(detail.getActivityNumber(), 50, false, "OC-01-001-01-15-004", "活动项目号");
                AssertUtils.lengthCheck(detail.getShippingWarehouseCode(), 50, false, "OC-01-001-01-15-004", "发货仓编码");
                AssertUtils.lengthCheck(detail.getDeliveryFactoryCode(), 50, false, "OC-01-001-01-15-004", "发货工厂编码");
                AssertUtils.lengthCheck(detail.getStockCode(), 50, false, "OC-01-001-01-15-004", "发货库存地点编码");
                AssertUtils.lengthCheck(detail.getSourceUnit(), 50, false, "OC-01-001-01-15-004", "来单单位");
//                AssertUtils.lengthCheck(detail.getSourceQuantity() == null ? "" : String.valueOf(detail.getSourceQuantity()), 10, false, "OC-01-001-01-15-004", "来单数量");
//                AssertUtils.lengthCheck(detail.getSourcePrice() == null ? "" : String.valueOf(detail.getSourcePrice()), 10, false, "OC-01-001-01-15-004", "来单价格");
                AssertUtils.isTrue(new BigDecimal(INTEGER_MAX_VALUE).compareTo(new BigDecimal(detail.getQuantity())) < 0, "OC-01-001-01-15-006", "预报数量", "最大2147483647");
                AssertUtils.isTrue(BigDecimal.ZERO.compareTo(new BigDecimal(detail.getQuantity())) >= 0, "OC-01-001-01-15-006", "预报数量", "不允许为零或负数");
                // 校验扩展字段长度和格式
                AssertUtils.lengthCheck(detail.getBizFeatures(), 1024, false, "OC-01-001-01-15-004", "扩展字段");
                AssertUtils.isTrue(!JsonUtils.isValidJson(detail.getBizFeatures()), "OC-00-001-01-15-001", "扩展字段");

                // 集团销售预报订单校验字段
                AssertUtils.lengthCheck(detail.getGoodsProductationTime(), 10, false, "OC-01-001-01-15-004", "生产日期");
                if (StringUtils.isNotBlank(detail.getGoodsProductationTime())) {
                    AssertUtils.isTrue(!DateUtils.isDateFormat(detail.getGoodsProductationTime()), "OC-00-001-01-15-001", "生产日期");
                }
                if (Objects.nonNull(detail.getUnitPrice())) {
                    AssertUtils.isTrue(new BigDecimal(INTEGER_MAX_VALUE).compareTo(new BigDecimal(detail.getUnitPrice())) < 0, "OC-01-001-01-15-006", "单价", "最大2147483647");
                    AssertUtils.isTrue(BigDecimal.ZERO.compareTo(new BigDecimal(detail.getUnitPrice())) >= 0, "OC-01-001-01-15-006", "单价", "不允许为零或负数");
                }
                if (Objects.nonNull(detail.getTotalActivityFee())) {
                    AssertUtils.lengthCheck(String.valueOf(detail.getTotalActivityFee()/100), 10, false, "OC-01-001-01-15-004", "折扣后总金额");
                    AssertUtils.isTrue(new BigDecimal(INTEGER_MAX_VALUE).compareTo(new BigDecimal(detail.getTotalActivityFee() / 100)) < 0, "OC-01-001-01-15-006", "折扣后总金额", "最大2147483647");
                    AssertUtils.isTrue(BigDecimal.ZERO.compareTo(new BigDecimal(detail.getTotalActivityFee())) >= 0, "OC-01-001-01-15-006", "折扣后总金额", "不允许为零或负数");
                }
                if (Objects.nonNull(detail.getSourceQuantity())) {
                    AssertUtils.isWholeNumber(String.valueOf(detail.getSourceQuantity()), false, "OC-01-001-01-15-006", "来单数量");
                    AssertUtils.isTrue(new BigDecimal(INTEGER_MAX_VALUE).compareTo(new BigDecimal(detail.getSourceQuantity())) < 0, "OC-01-001-01-15-006", "来单数量", "最大2147483647");
                    AssertUtils.isTrue(BigDecimal.ZERO.compareTo(new BigDecimal(detail.getSourceQuantity())) >= 0, "OC-01-001-01-15-006", "来单数量", "不允许为零或负数");
                }
                if (Objects.nonNull(detail.getSourcePrice())) {
//                    AssertUtils.isAmountNumber(String.valueOf(detail.getSourcePrice()), false, "OC-01-001-01-15-006", "来单价格");
//                    AssertUtils.isTrue(new BigDecimal(INTEGER_MAX_VALUE).compareTo(new BigDecimal(detail.getSourcePrice())) < 0, "OC-01-001-01-15-006", "来单价格", "最大2147483647");
                    AssertUtils.isTrue(BigDecimal.ZERO.compareTo(detail.getSourcePrice()) >= 0, "OC-01-001-01-15-006", "来单价格", "不允许为零或负数");
                }
            });
        });
    }

    public static void paramsEnumDictCheck(List<ForecastOrderEntity> forecastOrderEntities) {
        // 从数据字典里获取来源系统信息
        List<DictionaryResponse> fromSysResponseList = forecastOrderAbility.baseDataServiceAdapter.queryDictionarys(DictionaryTypeEnum.FROM_SYS.getCode());
        Map<String, DictionaryResponse> fromSysMap = fromSysResponseList.stream().collect(Collectors.toMap(DictionaryResponse::getCode, Function.identity(), (k1, k2) -> k2));

        // 从数据字典里获取业态信息
        List<DictionaryResponse> businessTypeResponseList = forecastOrderAbility.baseDataServiceAdapter.queryDictionarys(DictionaryTypeEnum.BUSINESS_TYPE.getCode());
        Map<String, DictionaryResponse> businessTypeMap = businessTypeResponseList.stream().collect(Collectors.toMap(DictionaryResponse::getCode, Function.identity(), (k1, k2) -> k2));

        // 从数据字典里获取业务单元编码
        List<DictionaryResponse> businessUnitList = forecastOrderAbility.baseDataServiceAdapter.queryDictionarys(DictionaryTypeEnum.BUSINESS_UNIT_CODE.getCode());
        Map<String, DictionaryResponse> businessUnitMap = businessUnitList.stream().collect(Collectors.toMap(DictionaryResponse::getCode, Function.identity(), (k1, k2) -> k2));

        List<DictionaryResponse> fulfillmentTypeList = forecastOrderAbility.baseDataServiceAdapter.queryDictionarys(DictionaryTypeEnum.GROUP_SALE_FULFILLMENT_TYPE.getCode());
        Map<String, DictionaryResponse> fulfillmentTypeMap = fulfillmentTypeList.stream().collect(Collectors.toMap(DictionaryResponse::getCode, Function.identity(), (k1, k2) -> k2));
        //履约方式
        List<DictionaryResponse> fulfillmentTypeList1 = forecastOrderAbility.baseDataServiceAdapter.queryDictionarys(DictionaryTypeEnum.COMPANY_SALE_FULFILLMENT_TYPE.getCode());
        Map<String, DictionaryResponse> fulfillmentTypeMap1 = fulfillmentTypeList1.stream().collect(Collectors.toMap(DictionaryResponse::getCode, Function.identity(), (k1, k2) -> k2));
        forecastOrderEntities.forEach(forecastOrderEntity -> {
            ForecastOrderBO forecastOrderBO = forecastOrderEntity.getForecastOrderBO();
            if (Objects.isNull(fromSysMap.get(forecastOrderBO.getFromSys()))) {
                throw new FunctionException("OC-01-001-01-15-006", "来源系统编码");
            }
            if (!"false".equals(forecastOrderBO.getSubmitFlag()) && !"true".equals(forecastOrderBO.getSubmitFlag())) {
                throw new FunctionException("OC-01-001-01-15-006", "是否提交");
            }

            DictionaryResponse businessType = businessTypeMap.get(forecastOrderBO.getBusinessType());
            if (Objects.isNull(businessType)) {
                throw new FunctionException("OC-01-001-01-15-006", "业态编码");
            }
            // 覆盖用户传入数据
            forecastOrderBO.setBusinessTypeName(businessType.getName());

            DictionaryResponse businessUnitCode = businessUnitMap.get(forecastOrderBO.getBusinessUnitCode());
            if (Objects.isNull(businessUnitCode)) {
                throw new FunctionException("OC-01-001-01-15-006", "业务单元编码");
            }
            forecastOrderBO.setBusinessUnitName(businessUnitCode.getName());

            // 校验订单类型编码
            if (OrderCategoryEnum.GROUP.getCode().equals(forecastOrderBO.getOrderCategoryCode())) {
                if (!ForecastOrderTypeEnum.GROUP_FORECAST.getCode().equals(forecastOrderBO.getOrderTypeCode())) {
                    throw new FunctionException("OC-01-001-01-15-006", "订单类型编码");
                }
            } else {
                if (!ForecastOrderTypeEnum.STD_FORECAST.getCode().equals(forecastOrderBO.getOrderTypeCode())
                        && !ForecastOrderTypeEnum.REPLENISH_FORECAST.getCode().equals(forecastOrderBO.getOrderTypeCode())) {
                    throw new FunctionException("OC-01-001-01-15-006", "订单类型编码");
                }
            }
            ForecastOrderTypeEnum forecastOrderTypeEnum = ForecastOrderTypeEnum.of(forecastOrderBO.getOrderTypeCode());
            Optional.ofNullable(forecastOrderTypeEnum).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "订单类型编码"));
            // 设置订单类型名称和订单类型值
            forecastOrderBO.setOrderTypeName(forecastOrderTypeEnum.getDesc());
            forecastOrderBO.setOrderType(forecastOrderTypeEnum.getValue());

            // 校验订单渠道编码
            if (StringUtils.isNotBlank(forecastOrderBO.getOrderChannelCode())) {
                OrderChannelEnum orderChannelEnum = OrderChannelEnum.of(forecastOrderBO.getOrderChannelCode());
                Optional.ofNullable(orderChannelEnum).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "订单渠道编码"));
            }

            // 校验履约优先级
            if (StringUtils.isNotBlank(forecastOrderBO.getFulfillmentPriority())) {
                FulfillmentPriorityEnum fulfillmentPriorityEnum = FulfillmentPriorityEnum.of(forecastOrderBO.getFulfillmentPriority());
                Optional.ofNullable(fulfillmentPriorityEnum).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "履约优先级"));
            }

            // 识别是否为集团销售预报订单
            boolean isGroup = forecastOrderTypeEnum.getCode().equals(ForecastOrderTypeEnum.GROUP_FORECAST.getCode());
            if (isGroup) {
                // 校验履约方式编码
                if (StringUtils.isNotBlank(forecastOrderBO.getFulfillmentTypeCode())) {
                    DictionaryResponse dictionaryResponse = fulfillmentTypeMap.get(forecastOrderBO.getFulfillmentTypeCode());
                    Optional.ofNullable(dictionaryResponse).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "履约方式编码"));
                }
            } else {
                // 校验履约方式编码
                if (StringUtils.isNotBlank(forecastOrderBO.getFulfillmentTypeCode())) {
                    DictionaryResponse dictionaryResponse = fulfillmentTypeMap1.get(forecastOrderBO.getFulfillmentTypeCode());
                    Optional.ofNullable(dictionaryResponse).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "履约方式编码"));
                }

            }
            if (!isGroup) {
                // 活动校验
                boolean isReplenishment = forecastOrderBO.getOrderTypeCode().equals(ForecastOrderTypeEnum.REPLENISH_FORECAST.getCode());
                if (isReplenishment) {
                    // 补货预报订单必须传活动信息
                    forecastOrderBO.getForecastOrderLineBOList().forEach(detail -> {
                        AssertUtils.isTrue(StringUtils.isBlank(detail.getActivityCode()), "OC-01-001-01-15-001", "补货活动编码");
                        AssertUtils.isTrue(StringUtils.isBlank(detail.getActivityNumber()), "OC-01-001-01-15-001", "补货活动项目号");
                    });
                } else {
                    forecastOrderBO.getForecastOrderLineBOList().forEach(detail -> {
                        AssertUtils.isTrue(StringUtils.isNotBlank(detail.getActivityCode()), "OC-01-001-01-15-008", "补货活动编码");
                        AssertUtils.isTrue(StringUtils.isNotBlank(detail.getActivityNumber()), "OC-01-001-01-15-008", "补货活动项目号");
                    });
                }
            }

            // 校验配送方式编码
            SalesOrderDeliveryMethodEnum salesOrderDeliveryMethodEnum = SalesOrderDeliveryMethodEnum.of(forecastOrderBO.getTransportModeCode());
            Optional.ofNullable(salesOrderDeliveryMethodEnum).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "配送方式编码"));
            // 设置配送方式名称
            forecastOrderBO.setTransportModeName(salesOrderDeliveryMethodEnum.getDesc());

            // 校验订单原因编码
            if (StringUtils.isNotBlank(forecastOrderBO.getCreateReasonCode())) {
                SalesOrderReasonEnum salesOrderReasonEnum = SalesOrderReasonEnum.of(forecastOrderBO.getCreateReasonCode());
                Optional.ofNullable(salesOrderReasonEnum).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "订单原因编码"));
                // 设置订单原因名称
                forecastOrderBO.setCreateReasonName(salesOrderReasonEnum.getDesc());
            }

            if (StringUtils.isNotBlank(forecastOrderBO.getCustomerGroup())) {
                CustomerGroupEnum customerGroupEnum = CustomerGroupEnum.of(forecastOrderBO.getCustomerGroup());
                if (Objects.nonNull(customerGroupEnum)) {
                    // 设置客户组名称
                    forecastOrderBO.setCustomerGroupName(customerGroupEnum.getDesc());
                }
            }

            // 校验扩展字段是否为JSON格式
            AssertUtils.isTrue(!JsonUtils.isValidJson(forecastOrderBO.getBizFeatures()), "OC-00-001-01-15-001", "扩展字段");

            for (ForecastOrderLineBO p : forecastOrderBO.getForecastOrderLineBOList()) {
                StockUnitEnum salesUnit = StockUnitEnum.of(p.getSaleUnitCode());
                Optional.ofNullable(salesUnit).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "销售单位编码"));
                p.setSaleUnitName(salesUnit.getDesc());

                // 校验扩展字段是否为JSON格式
                AssertUtils.isTrue(!JsonUtils.isValidJson(p.getBizFeatures()), "OC-00-001-01-15-001", "扩展字段");
            }
        });
    }

    public Boolean idempotentProcess(String sequenceNo, ForecastOrderBO forecastOrderBO, List<CreateForecastResponseDTO> responseList) {
        try {
            String reqNo = forecastOrderBO.getReqNo();
            //数据幂等
            QueryBySourceNoRequest sourceNoRequest = new QueryBySourceNoRequest();
            sourceNoRequest.setFromSys(forecastOrderBO.getFromSys());
            sourceNoRequest.setUnitCode(forecastOrderBO.getBusinessUnitCode());
            sourceNoRequest.setBusinessTypeCode(forecastOrderBO.getBusinessType());
            sourceNoRequest.setReqNo(forecastOrderBO.getReqNo());
            ForecastOrderSDO existForecastOrderSDO = forecastOrderQueryRepository.queryBySourceOrderNo(sourceNoRequest);
            log.info("forecast_idempotent_DB_exist_request={}", JSON.toJSONString(sourceNoRequest));
            // 根据请求号进行幂等处理，当请求号已存在-> 幂等返回已经存在数据，不做更新
            if (Objects.nonNull(existForecastOrderSDO)) {
                log.info("forecast_idempotent_DB_exist_response={}", existForecastOrderSDO.getSourceOrderNo());
                //追加到返回列表，并且不再对该数据进行后续处理
                addIdempotentOrderSuccessResponse(responseList, existForecastOrderSDO, reqNo, sequenceNo);
                return Boolean.FALSE;
            }
            return Boolean.TRUE;
        } catch (Exception e) {
            log.warn("未获取到任务信息返回空", e);
            return Boolean.TRUE;
        }
    }

    public void addIdempotentOrderSuccessResponse(List<CreateForecastResponseDTO> responseList, ForecastOrderSDO forecastOrderSDO, String seqNo, String taskId) {
        List<CreateForecastDetailResponseDTO> forecastDetailResponseDTOS = Optional.ofNullable(forecastOrderSDO.getForecastOrderLineSDOList())
                .orElse(Lists.newArrayList()).stream()
                .map(ForecastOrderLineSDOToCreateForecastDetailResponseDTOConvertor::convert)
                .collect(Collectors.toList());

        CreateForecastResponseDTO response = new CreateForecastResponseDTO();
        response.setSalesOrderNo(forecastOrderSDO.getOrderBizId());
        response.setSourceOrderNo(forecastOrderSDO.getSourceOrderNo());
        response.setForecastDetailResponseDTOS(forecastDetailResponseDTOS);
        response.setReqNo(seqNo);
        response.setBatchTaskId(taskId);
        response.setSuccess(Boolean.TRUE);
        responseList.add(response);
    }

    public static void queryAnalysisCheck(ForecastOrderBizQueryAnalysisRequest request) {
        // 基础入参校验
        baseParamsCheck(request.getFromSys(), request.getBusinessType(), request.getBusinessUnitCode());

        // 必填校验
        AssertUtils.isTrue(StringUtils.isBlank(request.getBatchNo()), "OC-01-001-01-15-001", "批次号");

        // 长度校验
        AssertUtils.lengthCheck(request.getBatchNo(), 50, true, "OC-01-001-01-15-004", "批次号");
    }

    public static void replyModifyRequestCheck(ForecastReplyBizBatchModifyRequest request) {
        // 基础入参校验
        baseParamsCheck(request.getFromSys(), request.getBusinessType(), request.getBusinessUnitCode());

        // 必填校验
        List<ForecastReplyBizModifyRequest> orderList = request.getOrderList();
        AssertUtils.isTrue(StringUtils.isBlank(request.getModifierCode()), "OC-01-001-01-15-001", "修改人工号");
        AssertUtils.isTrue(CollectionUtils.isEmpty(orderList), "OC-01-001-01-15-001", "单据列表");

        // 长度校验
        AssertUtils.isTrue(orderList.size() > 100, "OC-01-001-01-15-004", "单据列表", orderList.size(), "100");
        AssertUtils.lengthCheck(request.getModifierCode(), 50, true, "OC-01-001-01-15-004", "修改人工号");

        // 入参数据重复校验
        Set<String> orderSet = new HashSet<>();
        orderList.forEach(order -> {
            AssertUtils.isTrue(StringUtils.isBlank(order.getSalesOrderNo()), "OC-01-001-01-15-001", "预报订单单号");
            AssertUtils.lengthCheck(order.getSalesOrderNo(), 50, true, "OC-01-001-01-15-004", "预报订单单号");
            AssertUtils.isTrue(StringUtils.isBlank(order.getLineNum()), "OC-01-001-01-15-001", "行号");
            AssertUtils.lengthCheck(order.getLineNum(), 50, true, "OC-01-001-01-15-004", "行号");
            AssertUtils.isTrue(StringUtils.isBlank(order.getPredictReplyAmount()), "OC-01-001-01-15-001", "预计回复数量");
            AssertUtils.lengthCheck(order.getPredictReplyAmount(), 10, "OC-01-001-01-15-004", "预计回复数量");
            AssertUtils.isNumber(order.getPredictReplyAmount(), true, "OC-01-001-01-15-006", "预计回复数量");
            log.info("order.getBizFeatures()长度:{}", org.apache.commons.lang3.StringUtils.length(order.getBizFeatures()));
//            if(StringUtils.isNotBlank(order.getBizFeatures())) {
//                int length = order.getBizFeatures().getBytes(StandardCharsets.UTF_8).length;
//                if(length > 1024){
//                    throw new FunctionException("OC-01-001-01-15-004", "扩展字段", length, 1024);
//                }
////                    AssertUtils.lengthCheck(bizFeatures, 1024, false, "OC-01-001-01-15-004", "扩展字段");
//            }
            AssertUtils.lengthCheck(order.getBizFeatures(), 1024, false, "OC-01-001-01-15-004", "扩展字段");
            AssertUtils.isTrue(!JsonUtils.isValidJson(order.getBizFeatures()), "OC-00-001-01-15-001", "扩展字段");
            AssertUtils.isTrue(!orderSet.add(String.format("%s-%s", order.getSalesOrderNo(), order.getLineNum())), "OC-01-001-01-15-002", "单据列表", order.getSalesOrderNo());
        });
    }

    private static void modifyActivityLengthCheck(ForecastOrderBizBatchModifyActivityRequest request) {
        AssertUtils.isTrue(request.getActivityList().size() > 100, "OC-01-001-01-15-004", "activityList", request.getActivityList().size(), "100");
        request.getActivityList().forEach(activity -> {
            AssertUtils.lengthCheck(activity.getSalesOrderNo(), 50, true, "OC-01-001-01-15-004", "预报单单号");
            AssertUtils.lengthCheck(activity.getLineNum(), 10, true, "OC-01-001-01-15-004", "行号");
            if (CollectionUtils.isEmpty(activity.getDetailActivityList())) {
                return;
            }
            AssertUtils.isTrue(activity.getDetailActivityList().size() > 100, "OC-01-001-01-15-004", "detailActivityList", activity.getDetailActivityList().size(), "100");
            activity.getDetailActivityList().forEach(detailActivity -> {
                AssertUtils.lengthCheck(detailActivity.getActivityId(), 50, true, "OC-01-001-01-15-004", "活动ID");
                AssertUtils.lengthCheck(detailActivity.getActivityLineNo(), 50, true, "OC-01-001-01-15-004", "活动项目号");
                AssertUtils.lengthCheck(detailActivity.getActivityType(), 50, true, "OC-01-001-01-15-004", "活动类型");
                if (CollectionUtils.isEmpty(detailActivity.getGiftList())) {
                    return;
                }
                AssertUtils.isTrue(detailActivity.getGiftList().size() > 100, "OC-01-001-01-15-004", "detailActivityList", detailActivity.getGiftList().size(), "100");
                detailActivity.getGiftList().forEach(gift -> {
                    AssertUtils.isWholeNumber(gift.getGiftNumber(), true, "OC-01-001-01-15-006", "赠品数量");

                    AssertUtils.lengthCheck(gift.getGiftCode(), 50, true, "OC-01-001-01-15-004", "赠品编码");
                    AssertUtils.lengthCheck(gift.getGiftNumber(), 10, true, "OC-01-001-01-15-004", "赠品数量");
                    AssertUtils.lengthCheck(gift.getGiftSalesUnitCode(), 50, true, "OC-01-001-01-15-004", "赠品销售单位编码");
                    AssertUtils.lengthCheck(gift.getGiftDisable(), 50, true, "OC-01-001-01-15-004", "赠品禁止选中标识");
                    AssertUtils.lengthCheck(gift.getNote(), 255, false, "OC-01-001-01-15-004", "赠品备注");
                    if (StringUtils.isNotBlank(gift.getGiftNumber())) {
                        AssertUtils.isTrue(new BigDecimal(INTEGER_MAX_VALUE).compareTo(new BigDecimal(gift.getGiftNumber())) < 0, "OC-01-001-01-15-009", "赠品数量", "最大2147483647");
                        AssertUtils.isTrue(BigDecimal.ZERO.compareTo(new BigDecimal(gift.getGiftNumber())) >= 0, "OC-01-001-01-15-009", "赠品数量", "不允许为零或负数");
                    }
                });
            });
        });
    }

    private static void modifyActivityBlankCheck(ForecastOrderBizBatchModifyActivityRequest request) {
        AssertUtils.isTrue(CollectionUtils.isEmpty(request.getActivityList()), "OC-01-001-01-15-001", "活动列表");
        request.getActivityList().forEach(activity -> {
            AssertUtils.isTrue(StringUtils.isBlank(activity.getSalesOrderNo()), "OC-01-001-01-15-001", "预报订单单号");
            AssertUtils.isTrue(StringUtils.isBlank(activity.getLineNum()), "OC-01-001-01-15-001", "行号");
            if (CollectionUtils.isEmpty(activity.getDetailActivityList())) {
                return;
            }
            activity.getDetailActivityList().forEach(detailActivity -> {
                AssertUtils.isTrue(StringUtils.isBlank(detailActivity.getActivityId()), "OC-01-001-01-15-001", "活动ID");
                AssertUtils.isTrue(StringUtils.isBlank(detailActivity.getActivityLineNo()), "OC-01-001-01-15-001", "活动项目号");
                AssertUtils.isTrue(StringUtils.isBlank(detailActivity.getActivityType()), "OC-01-001-01-15-001", "活动类型");
                if (CollectionUtils.isEmpty(detailActivity.getGiftList())) {
                    return;
                }
                detailActivity.getGiftList().forEach(gift -> {
                    AssertUtils.isTrue(StringUtils.isBlank(gift.getGiftCode()), "OC-01-001-01-15-001", "赠品编码");
                    AssertUtils.isTrue(StringUtils.isBlank(gift.getGiftNumber()), "OC-01-001-01-15-001", "赠品数量");
                    AssertUtils.isTrue(StringUtils.isBlank(gift.getGiftSalesUnitCode()), "OC-01-001-01-15-001", "赠品销售单位编码");
                    AssertUtils.isTrue(StringUtils.isBlank(gift.getGiftDisable()), "OC-01-001-01-15-001", "赠品禁止选中标识");
                });
            });
        });
    }

    public static void closeRequestCheck(ForecastOrderBizBatchCloseRequest request) {
        // 基础入参校验
        baseParamsCheck(request.getFromSys(), request.getBusinessType(), request.getBusinessUnitCode());

        // 必填校验
        AssertUtils.isTrue(StringUtils.isBlank(request.getModifierCode()), "OC-01-001-01-15-001", "修改人工号");
        AssertUtils.isTrue(CollectionUtils.isEmpty(request.getOrderList()), "OC-01-001-01-15-001", "单据明细");

        // 长度校验
        AssertUtils.lengthCheck(request.getModifierCode(), 50, true, "OC-01-001-01-15-004", "修改人工号");
        AssertUtils.isTrue(request.getOrderList().size() > 100, "OC-01-001-01-15-004", "单据列表", String.valueOf(request.getOrderList().size()), "100");

        // 重复值校验
        Set<String> sameLineNum = new HashSet<>();
        request.getOrderList().forEach(detail -> {
            AssertUtils.isTrue(StringUtils.isBlank(detail.getCloseCode()), "OC-01-001-01-15-001", "关闭原因编码");
            AssertUtils.isTrue(StringUtils.isBlank(detail.getSalesOrderNo()), "OC-01-001-01-15-001", "预报订单单号");
            AssertUtils.isTrue(StringUtils.isBlank(detail.getLineNum()), "OC-01-001-01-15-001", "行号");

            AssertUtils.lengthCheck(detail.getCloseCode(), 50, true, "OC-01-001-01-15-004", "关闭原因编码");
            AssertUtils.lengthCheck(detail.getSalesOrderNo(), 50, true, "OC-01-001-01-15-004", "预报订单单号");
            AssertUtils.lengthCheck(detail.getLineNum(), 50, true, "OC-01-001-01-15-004", "行号");
            String lineNum = detail.getSalesOrderNo() + "_" + detail.getLineNum();
            if (sameLineNum.contains(lineNum)) {
                throw new FunctionException("OC-01-001-01-15-002", detail.getSalesOrderNo(), detail.getLineNum());
            } else {
                sameLineNum.add(lineNum);
            }
            Optional.ofNullable(SalesOrderCloseEnum.of(detail.getCloseCode())).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "关闭原因编码"));
        });
    }

    public static void modifyActivityRequestCheck(ForecastOrderBizBatchModifyActivityRequest request) {
        // 基础入参校验
        baseParamsCheck(request.getFromSys(), request.getBusinessType(), request.getBusinessUnitCode());

        // 必填校验
        AssertUtils.isTrue(StringUtils.isBlank(request.getModifierCode()), "OC-01-001-01-15-001", "修改人工号");
        AssertUtils.isTrue(CollectionUtils.isEmpty(request.getActivityList()), "OC-01-001-01-15-001", "活动列表");

        // 长度校验
        AssertUtils.lengthCheck(request.getModifierCode(), 50, true, "OC-01-001-01-15-004", "修改人工号");
        //活动元素必填校验
        modifyActivityBlankCheck(request);
        //活动元素长度校验
        modifyActivityLengthCheck(request);
    }

    public static void queryActivityRequestCheck(ForecastOrderBizBatchQueryActivityRequest request) {
        // 基础入参校验
        baseParamsCheck(request.getFromSys(), request.getBusinessType(), request.getBusinessUnitCode());

        // 必填校验
        AssertUtils.isTrue(CollectionUtils.isEmpty(request.getOrderList()), "OC-01-001-01-15-001", "单据列表");

        // 长度校验
        request.getOrderList().forEach(order -> {
            AssertUtils.isTrue(StringUtils.isBlank(order.getSalesOrderNo()), "OC-01-001-01-15-001", "预报订单单号");
            AssertUtils.lengthCheck(order.getSalesOrderNo(), 50, true, "OC-01-001-01-15-004", "预报订单单号");
        });
    }

    public static void cancelRequestCheck(CancelForecastRequest request) {
        // 基础入参校验
        baseParamsCheck(request.getFromSys(), request.getBusinessType(), request.getBusinessUnitCode());

        // 必填校验
        List<ForecastCancelRequest> detailList = request.getCancelForecastDTOS();
        AssertUtils.isTrue(CollectionUtils.isEmpty(detailList), "OC-01-001-01-15-001", "单据列表");
        detailList.forEach(forecast -> {
            AssertUtils.isTrue(StringUtils.isBlank(forecast.getSalesOrderNo()), "OC-01-001-01-15-001", "预报订单单号");
            AssertUtils.isTrue(StringUtils.isBlank(forecast.getModifierCode()), "OC-01-001-01-15-001", "修改人工号");
        });

        // 长度校验
        AssertUtils.isTrue(detailList.size() > 100, "OC-01-001-01-15-004", "单据列表", String.valueOf(detailList.size()), "100");
        Set<String> orderSet = new HashSet<>();
        detailList.forEach(forecast -> {
            AssertUtils.lengthCheck(forecast.getSalesOrderNo(), 50, true, "OC-01-001-01-15-004", "预报订单单号");
            AssertUtils.lengthCheck(forecast.getModifierCode(), 50, true, "OC-01-001-01-15-004", "修改人工号");
            AssertUtils.lengthCheck(forecast.getCancelCode(), 50, false, "OC-01-001-01-15-004", "拒绝原因编码");

            if (orderSet.contains(forecast.getSalesOrderNo())) {
                throw new FunctionException("OC-01-001-01-15-002", "预报订单单号", forecast.getSalesOrderNo());
            }
            orderSet.add(forecast.getSalesOrderNo());
        });
        // 枚举校验
        detailList.forEach(forecast -> {
            if (StringUtils.isNotBlank(forecast.getCancelCode())) {
                SalesOrderCloseEnum salesOrderCloseEnum = SalesOrderCloseEnum.of(forecast.getCancelCode());
                AssertUtils.isTrue(Objects.isNull(salesOrderCloseEnum), "OC-01-001-01-15-006", "拒绝原因编码");
            }
        });
    }

    public static void pageQueryLineRequestCheck(PageQueryForecastLineRequest request) {
        if (Objects.isNull(request.getPage())) {
            request.setPage(1);
        }
        if (Objects.isNull(request.getSize())) {
            request.setSize(10);
        }

        AssertUtils.isTrue(StringUtils.isBlank(request.getBusinessType()), "OC-01-001-01-15-001", "业态编码");
        AssertUtils.isTrue(StringUtils.isBlank(request.getBusinessUnitCode()), "OC-01-001-01-15-001", "业务单元编码");
        AssertUtils.lengthCheck(request.getBusinessType(), 10, true, "OC-01-001-01-15-004", "业态编码");
        AssertUtils.lengthCheck(request.getBusinessUnitCode(), 50, true, "OC-01-001-01-15-004", "业务单元编码");
        AssertUtils.isTrue(request.getPage() > MnOrderCenterConstants.MAX_PAGE().intValue(), "OC-01-001-01-15-009", "页数", "最大值为" + MnOrderCenterConstants.MAX_PAGE().intValue());
        AssertUtils.isTrue(request.getSize() > MnOrderCenterConstants.MAX_SIZE().intValue(), "OC-01-001-01-15-009", "分页条数", "最大值为" + MnOrderCenterConstants.MAX_SIZE().intValue());
        AssertUtils.isTrue(request.getPage() <= 0, "OC-01-001-01-15-009", "页数", "最小值为1");
        AssertUtils.isTrue(request.getSize() <= 0, "OC-01-001-01-15-009", "分页条数", "最小值为1");

        forecastOrderAbility.dictionaryVerifyAdapter.verifyDictionary(DictionaryTypeEnum.BUSINESS_TYPE.getCode(), request.getBusinessType(), new FunctionException("OC-01-001-01-15-006", "业态编码"));
        AssertUtils.lengthCheck(request.getFromSys(), 50, false, "OC-01-001-01-15-004", "来源系统编码");
        if (StringUtils.isNotBlank(request.getFromSys())) {
            forecastOrderAbility.dictionaryVerifyAdapter.verifyDictionary(DictionaryTypeEnum.FROM_SYS.getCode(), request.getFromSys(), new FunctionException("OC-01-001-01-15-006", "来源系统编码"));
        }
        if (CollectionUtils.isNotEmpty(request.getSalesOrderNoList())) {
            AssertUtils.isTrue(request.getSalesOrderNoList().size() > 100, "OC-01-001-01-15-004", "预报订单单号", request.getSalesOrderNoList().size(), 100);
            request.getSalesOrderNoList().forEach(x -> {
                AssertUtils.lengthCheck(x, 50, false, "OC-01-001-01-15-004", "预报订单单号");
            });
        }
        if (CollectionUtils.isNotEmpty(request.getSourceOrderNoList())) {
            AssertUtils.isTrue(request.getSourceOrderNoList().size() > 100, "OC-01-001-01-15-004", "来源单号", request.getSourceOrderNoList().size(), 100);
            request.getSourceOrderNoList().forEach(x -> {
                AssertUtils.lengthCheck(x, 50, false, "OC-01-001-01-15-004", "来源单号");
            });
        }
        if (CollectionUtils.isNotEmpty(request.getOrderTypeCodeList())) {
            AssertUtils.isTrue(request.getOrderTypeCodeList().size() > 10, "OC-01-001-01-15-004", "订单类型编码", request.getOrderTypeCodeList().size(), 10);
            request.getOrderTypeCodeList().forEach(x -> {
                AssertUtils.lengthCheck(x, 50, false, "OC-01-001-01-15-004", "订单类型编码");
            });
        }
        if (CollectionUtils.isNotEmpty(request.getShippingWarehouseCodeList())) {
            AssertUtils.isTrue(request.getShippingWarehouseCodeList().size() > 10, "OC-01-001-01-15-004", "发货仓编码", request.getShippingWarehouseCodeList().size(), 10);
            request.getShippingWarehouseCodeList().forEach(x -> {
                AssertUtils.lengthCheck(x, 50, false, "OC-01-001-01-15-004", "发货仓编码");
            });
        }
        AssertUtils.lengthCheck(request.getDeliveryFactoryCode(), 50, false, "OC-01-001-01-15-004", "发货工厂编码");
        AssertUtils.lengthCheck(request.getStockCode(), 50, false, "OC-01-001-01-15-004", "发货库存地点编码");

        AssertUtils.lengthCheck(request.getCreateBeginTime(), 19, false, "OC-01-001-01-15-004", "创建开始日期");
        if (StringUtils.isNotBlank(request.getCreateBeginTime())) {
            AssertUtils.isTrue(!DateUtils.isDateTimeFormat(request.getCreateBeginTime()), "OC-00-001-01-15-001", "创建开始日期");
        }
        AssertUtils.lengthCheck(request.getCreateEndTime(), 19, false, "OC-01-001-01-15-004", "创建结束日期");
        if (StringUtils.isNotBlank(request.getCreateEndTime())) {
            AssertUtils.isTrue(!DateUtils.isDateTimeFormat(request.getCreateEndTime()), "OC-00-001-01-15-001", "创建结束日期");
        }
        Date startDate = DateUtils.getBeginOrEndByStr(request.getCreateBeginTime(), true);
        Date endDate = DateUtils.getBeginOrEndByStr(request.getCreateEndTime(), false);
        if (startDate != null && endDate != null) {
            AssertUtils.isTrue(startDate.after(endDate), "OC-01-001-01-15-011");
        }
        request.setCreateBeginTime(DateUtils.toDateStr(startDate, DateUtils.DATETIME_FORMAT));
        request.setCreateEndTime(DateUtils.toDateStr(endDate, DateUtils.DATETIME_FORMAT));

        AssertUtils.lengthCheck(request.getExpectDeliveryBeginDate(), 10, false, "OC-01-001-01-15-004", "期望交货开始日期");
        if (StringUtils.isNotBlank(request.getExpectDeliveryBeginDate())) {
            AssertUtils.isTrue(!DateUtils.isDateFormat(request.getExpectDeliveryBeginDate()), "OC-00-001-01-15-001", "期望交货开始日期");
        }
        AssertUtils.lengthCheck(request.getExpectDeliveryEndDate(), 10, false, "OC-01-001-01-15-004", "期望交货结束日期");
        if (StringUtils.isNotBlank(request.getExpectDeliveryEndDate())) {
            AssertUtils.isTrue(!DateUtils.isDateFormat(request.getExpectDeliveryEndDate()), "OC-00-001-01-15-001", "期望交货结束日期");
        }
    }

    public static void pageQueryRequestCheck(PageQueryForecastRequest request) {
        if (request.getPage() == null) {
            request.setPage(1);
        }
        if (request.getSize() == null) {
            request.setSize(10);
        }
        AssertUtils.isTrue(StringUtils.isBlank(request.getBusinessType()), "OC-01-001-01-15-001", "业态编码");
        AssertUtils.isTrue(StringUtils.isBlank(request.getBusinessUnitCode()), "OC-01-001-01-15-001", "业务单元编码");
        AssertUtils.lengthCheck(request.getBusinessType(), 10, true, "OC-01-001-01-15-004", "业态编码");
        AssertUtils.lengthCheck(request.getBusinessUnitCode(), 50, true, "OC-01-001-01-15-004", "业务单元编码");
        AssertUtils.isTrue(request.getPage() > MnOrderCenterConstants.MAX_PAGE().intValue(), "OC-01-001-01-15-009", "页数", "最大值为" + MnOrderCenterConstants.MAX_PAGE().intValue());
        AssertUtils.isTrue(request.getSize() > MnOrderCenterConstants.MAX_SIZE().intValue(), "OC-01-001-01-15-009", "分页条数", "最大值为" + MnOrderCenterConstants.MAX_SIZE().intValue());
        AssertUtils.isTrue(request.getPage() <= 0, "OC-01-001-01-15-009", "页数", "最小值为1");
        AssertUtils.isTrue(request.getSize() <= 0, "OC-01-001-01-15-009", "分页条数", "最小值为1");

        forecastOrderAbility.dictionaryVerifyAdapter.verifyDictionary(DictionaryTypeEnum.BUSINESS_TYPE.getCode(), request.getBusinessType(), new FunctionException("OC-01-001-01-15-006", "业态编码"));
        AssertUtils.lengthCheck(request.getFromSys(), 50, false, "OC-01-001-01-15-004", "来源系统编码");
        if (StringUtils.isNotBlank(request.getFromSys())) {
            forecastOrderAbility.dictionaryVerifyAdapter.verifyDictionary(DictionaryTypeEnum.FROM_SYS.getCode(), request.getFromSys(), new FunctionException("OC-01-001-01-15-006", "来源系统编码"));
        }
        if (CollectionUtils.isNotEmpty(request.getSalesOrderNoList())) {
            AssertUtils.isTrue(request.getSalesOrderNoList().size() > 100, "OC-01-001-01-15-004", "预报订单单号", request.getSalesOrderNoList().size(), 100);
            request.getSalesOrderNoList().forEach(x -> {
                AssertUtils.lengthCheck(x, 50, false, "OC-01-001-01-15-004", "预报订单单号");
            });
        }
        if (CollectionUtils.isNotEmpty(request.getSourceOrderNoList())) {
            AssertUtils.isTrue(request.getSourceOrderNoList().size() > 100, "OC-01-001-01-15-004", "来源单号", request.getSourceOrderNoList().size(), 100);
            request.getSourceOrderNoList().forEach(x -> {
                AssertUtils.lengthCheck(x, 50, false, "OC-01-001-01-15-004", "来源单号");
            });
        }
        if (CollectionUtils.isNotEmpty(request.getOrderTypeCodeList())) {
            AssertUtils.isTrue(request.getOrderTypeCodeList().size() > 10, "OC-01-001-01-15-004", "订单类型编码", request.getOrderTypeCodeList().size(), 10);
            request.getOrderTypeCodeList().forEach(x -> {
                AssertUtils.lengthCheck(x, 50, false, "OC-01-001-01-15-004", "订单类型编码");
            });
        }
        if (CollectionUtils.isNotEmpty(request.getShippingWarehouseCodeList())) {
            AssertUtils.isTrue(request.getShippingWarehouseCodeList().size() > 10, "OC-01-001-01-15-004", "发货仓编码", request.getShippingWarehouseCodeList().size(), 10);
            request.getShippingWarehouseCodeList().forEach(x -> {
                AssertUtils.lengthCheck(x, 50, false, "OC-01-001-01-15-004", "发货仓编码");
            });
        }
        AssertUtils.lengthCheck(request.getDeliveryFactoryCode(), 50, false, "OC-01-001-01-15-004", "发货工厂编码");
        AssertUtils.lengthCheck(request.getStockCode(), 50, false, "OC-01-001-01-15-004", "发货库存地点编码");
//        AssertUtils.lengthCheck(request.getCustomerCode(), 50, false, "OC-01-001-01-15-004", "客户售达方编码");
//        AssertUtils.lengthCheck(request.getShipToPartyCode(), 50, false, "OC-01-001-01-15-004", "客户送达方编码");
//        AssertUtils.lengthCheck(request.getSalesOrganizationCode(), 50, false, "OC-01-001-01-15-004", "销售组织编码");
//        AssertUtils.lengthCheck(request.getSalesDepartmentCode(), 50, false, "OC-01-001-01-15-004", "销售部门编码");
//        AssertUtils.lengthCheck(request.getDeliveryMethodCode(), 50, false, "OC-01-001-01-15-004", "配送方式编码");
//        AssertUtils.lengthCheck(request.getCreatorCode(), 50, false, "OC-01-001-01-15-004", "创建人编码");
//        AssertUtils.lengthCheck(request.getModifierCode(), 50, false, "OC-01-001-01-15-004", "修改人编码");
//        AssertUtils.lengthCheck(request.getCustomerPurchaseOrderNo(), 50, false, "OC-01-001-01-15-004", "客户采购订单号");
//        AssertUtils.lengthCheck(request.getOrderReasonCode(), 50, false, "OC-01-001-01-15-004", "订单原因编码");
//        AssertUtils.lengthCheck(request.getReceiveWarehouseCode(), 50, false, "OC-01-001-01-15-004", "收货仓（逻辑仓库编码）");
//        AssertUtils.lengthCheck(request.getContact(), 50, false, "OC-01-001-01-15-004", "联系人");
//        AssertUtils.lengthCheck(request.getContactPhone(), 50, false, "OC-01-001-01-15-004", "联系电话");
//        AssertUtils.lengthCheck(request.getCloseReasonCode(), 50, false, "OC-01-001-01-15-004", "拒绝原因编码");
//        AssertUtils.lengthCheck(request.getCreateBeginTime(), 19, false, "OC-01-001-01-15-004", "创建开始日期");
//        AssertUtils.lengthCheck(request.getCreateEndTime(), 19, false, "OC-01-001-01-15-004", "创建结束日期");
//        AssertUtils.lengthCheck(request.getCustomerGrade(), 50, false, "OC-01-001-01-15-004", "客户等级");
//        AssertUtils.lengthCheck(request.getCustomerGroup(), 50, false, "OC-01-001-01-15-004", "客户组");
//        AssertUtils.lengthCheck(request.getFulfillmentTypeCode(), 50, false, "OC-01-001-01-15-004", "履约方式");
//        AssertUtils.lengthCheck(request.getFulfillmentPriority(), 50, false, "OC-01-001-01-15-004", "履约优先级");
//        AssertUtils.lengthCheck(request.getOrderBizTag(), 50, false, "OC-01-001-01-15-004", "订单标签");
//        if (CollectionUtils.isNotEmpty(request.getSaleChannelCodeList())) {
//            AssertUtils.isTrue(request.getSaleChannelCodeList().size() > 10, "OC-01-001-01-15-004", "销售渠道编码", request.getSaleChannelCodeList().size(), 10);
//            request.getSaleChannelCodeList().forEach(x -> {
//                AssertUtils.lengthCheck(x, 50, false, "OC-01-001-01-15-004", "销售渠道编码");
//            });
//        }

        Date startDate = DateUtils.getBeginOrEndByStr(request.getCreateBeginTime(), true);
        Date endDate = DateUtils.getBeginOrEndByStr(request.getCreateEndTime(), false);
        if (startDate != null && endDate != null) {
            AssertUtils.isTrue(startDate.after(endDate), "OC-01-001-01-15-011");
        }
        //传入的时间不为空，但格式化后为空， 说明时间格式化失败 报错出去
        AssertUtils.isTrue(StringUtils.isNotBlank(request.getCreateBeginTime()) && startDate == null, "OC-00-001-01-15-001", "开始时间");
        AssertUtils.isTrue(StringUtils.isNotBlank(request.getCreateEndTime()) && endDate == null, "OC-00-001-01-15-001", "结束时间");
        request.setCreateBeginTime(DateUtils.toDateStr(startDate, DateUtils.DATETIME_FORMAT));
        request.setCreateEndTime(DateUtils.toDateStr(endDate, DateUtils.DATETIME_FORMAT));

//        AssertUtils.lengthCheck(request.getUpdateBeginTime(), 19, false, "OC-01-001-01-15-004", "修改开始日期");
//        AssertUtils.lengthCheck(request.getUpdateEndTime(), 19, false, "OC-01-001-01-15-004", "修改结束日期");
//        Date updateStartDate = DateUtils.getBeginOrEndByStr(request.getUpdateBeginTime(), true);
//        Date updateEndDate = DateUtils.getBeginOrEndByStr(request.getUpdateEndTime(), false);
//        if (updateStartDate != null && updateEndDate != null) {
//            AssertUtils.isTrue(updateStartDate.after(updateEndDate), "OC-01-001-01-15-011");
//        }
//        //传入的时间不为空，但格式化后为空， 说明时间格式化失败 报错出去
//        AssertUtils.isTrue(StringUtils.isNotBlank(request.getUpdateBeginTime()) && updateStartDate == null, "OC-00-001-01-15-001", "修改开始日期");
//        AssertUtils.isTrue(StringUtils.isNotBlank(request.getUpdateEndTime()) && updateEndDate == null, "OC-00-001-01-15-001", "修改结束日期");
//        request.setUpdateBeginTime(DateUtils.toDateStr(updateStartDate, DateUtils.DATETIME_FORMAT));
//        request.setUpdateEndTime(DateUtils.toDateStr(updateEndDate, DateUtils.DATETIME_FORMAT));
//
//        Date validStartDate = DateUtils.getBeginOrEndByStr(request.getVaildBeginTime(), true);
//        Date validEndDate = DateUtils.getBeginOrEndByStr(request.getValidEndTime(), false);
//        if (validStartDate != null && validEndDate != null) {
//            AssertUtils.isTrue(validStartDate.after(validEndDate), "OC-01-001-01-15-011");
//        }
//        //传入的时间不为空，但格式化后为空， 说明时间格式化失败 报错出去
//        AssertUtils.isTrue(StringUtils.isNotBlank(request.getVaildBeginTime()) && validStartDate == null, "OC-00-001-01-15-001", "生效开始日期");
//        AssertUtils.isTrue(StringUtils.isNotBlank(request.getValidEndTime()) && validEndDate == null, "OC-00-001-01-15-001", "生效结束日期");
//        request.setVaildBeginTime(DateUtils.toDateStr(validStartDate, DateUtils.DATETIME_FORMAT));
//        request.setValidEndTime(DateUtils.toDateStr(validEndDate, DateUtils.DATETIME_FORMAT));
//
//        Date expectedDeliveryStartDate = DateUtils.getBeginOrEndByStr(request.getExpectedDeliveryBeginTime(), true);
//        Date expectedDeliveryEndDate = DateUtils.getBeginOrEndByStr(request.getExpectedDeliveryEndTime(), false);
//        if (expectedDeliveryStartDate != null && expectedDeliveryEndDate != null) {
//            AssertUtils.isTrue(expectedDeliveryStartDate.after(expectedDeliveryEndDate), "OC-01-001-01-15-011");
//        }
//        //传入的时间不为空，但格式化后为空， 说明时间格式化失败 报错出去
//        AssertUtils.isTrue(StringUtils.isNotBlank(request.getExpectedDeliveryBeginTime()) && expectedDeliveryStartDate == null, "OC-00-001-01-15-001", "期望交货开始日期");
//        AssertUtils.isTrue(StringUtils.isNotBlank(request.getExpectedDeliveryEndTime()) && expectedDeliveryEndDate == null, "OC-00-001-01-15-001", "期望交货结束日期");
//        request.setExpectedDeliveryBeginTime(DateUtils.toDateStr(expectedDeliveryStartDate, DateUtils.DATETIME_FORMAT));
//        request.setExpectedDeliveryEndTime(DateUtils.toDateStr(expectedDeliveryEndDate, DateUtils.DATETIME_FORMAT));

    }
}
